Js中常用知识点(typeof、instanceof、动态属性、变量作用域)

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.color
     function 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>

猜你喜欢

转载自www.cnblogs.com/fllowerqq/p/8972153.html
今日推荐