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>

 

转载于:https://www.cnblogs.com/fllowerqq/p/8972153.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值