Js中常用知識點(typeof、動態屬性、變量作用域)
阿新 • • 發佈:2018-04-30
常用知識 sage 打印 bject 理解 各類 swa clas java
1、Js中各類型的常量表示形式:Number:number String:string Object:objec
2、typeof運算符在Js中的使用:用於判斷某一對象是何種類型,返回值是字符串(各類型的常量的字符串形式)
var message = "some string";
alert((typeof message) == String)
{
alert("OK");
} ——結果:false、ok
alert(typeof message == "string"); —true
3、typeof 不能精確的表示類的類型(例如下面這個例子:方法Student在這裏首字母大寫,表明聲明了一個類,而使用typeof運算符時卻打印出object)
function Student(n, a)
{
this.name = n;
this.age = a;
}
var stuA = new Student(‘張三‘, 23);
alert(typeof stuA)//object
alert(stuA.name + ":" + stuA.age); //張三:23
4、
5、 對象的動態屬性(無中生有),要求一定先聲明一個對象然後再有該對象的動態相屬性
6、理解Js中函數的作用域(importent)
<script type="text/javascript"> var color = "blue";//window.colorfunction changeColor(){ var anotherColor = "red"; //沒有使用var關鍵字,那麽它的作用域是Window,所以永遠不要這樣寫 myColor = "yellow"; //window.myColor function swapColors(){ var tempColor = anotherColor; anotherColor = color; color= tempColor; } swapColors(); } changeColor(); alert("Color is now " + color);//red alert(window.color); //red alert(window.myColor);//yellow </script>
Js中常用知識點(typeof、動態屬性、變量作用域)