数据类型和作用域链

数据类型

boolean number string null undefined object

typeof 测定数据类型

typeof a;

boolean number string object undefined object function

let n=12,s='12.34'
类型转换
let sn=Number(s);      //强制转换,12.34
let sn2=parseFloat(s);    //12.34
let sn3=+s               //12.34
let sn4=~~s            //12
let sn5=Math.floor(+s)  //转为整型

n+true                    //13

作用域链

let n1=new Number(3)
let n2=new Number(4)
判断n1和n2是否是同一个类型
console.log(n1);  //object
console.log(typeof n1=typeof n2);
console.log(typeof  n1);  //object

console.log(n1 instanceof Number);   //true
console.log(n1 instanceof Object);    //true
function User(){ //构造函数
this.name='tom'
}
let u1=new User();     //定义对象
User.prototype.age=0;   //在user的原型对象上定义了age 属性
Object.defineProperty(u1,'blood',{       //给u1对象定义一个blood属性,并且配置它
  value:'B',         //值
  writeable:true,    //是否可写
  configurable:true,   //是否可配置
  enumerable;true      //是否可写

})
for(let v in u1){           //遍历对象u1
  console.log(v);
}

delete u1.blood        //删除一个属性

任何一个类都是object的子类,任何一个类都有原型对象,至少有两个属性(constructor,proto

判断两个对象是否属于一个类,对象1.constructor===对象2.constructor

原型链

在一个对象里面遍历一个属性,遍历的顺序是,先遍历该类对象,再遍历该类所在的原型对象,再父类的原型对象,一直到object的原型对象。因为每个类中的原型对象都有一个constructor属性指向该类,也有一个-proto-属性指向父类的原型对象。

在JavaScript中原型是一个prototype对象,用于表示类型之间的关系

原型链的好处
一个方法所有的对象都可用,可以把它定义在object原型对象中
Data.prototype.define=function(){    //则Data类型的所有对象中都有define()方法  
}

一般方法定义在原型对象中,属性定义在类中,公共的属性也定义在原型对象中
es5中的继承,es5中没有真正的继承,用原型链来模拟继承
function Human(name,age){
  this.name=name;
  this.age=age;
}

Human.protopype.eat=function(){

}

function Student(school){
this.school=school
}


for(let k in Human.prototype){
Student.prototype[k]=Human.prototype[k];      
}

Student.prototype=Human.prototype;          //直接把student的原型对象变成了Human的,自己的就没有了

继承类中的属性用call或apply;
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值