JS原型

JS原型

var str = '123';//字面量
var str = new String('123');//构建一个字符串对象
console.log(str);

结果是object

var fn = function(){
    var a = 10;
    var b = 20;
}
console.log(fn instanceof Function);

结果是true(instanceof用于检测复杂的数据类型,返回的是布尔值)

原型对象

1.所有的函数都有prototype属性,该属性是一个对象

2.所有的对象都有一个proto属性,该属性也是一个对象

3.假如这个函数没有任何属性,prototype对象默认有个constructor属性

举个例子

   function fn(){
    fn.prototype.name = 'haha';//prototype给当前函数增加属性
    fn.prototype.age = 53;
    fn.prototype.xixi = function(){
        this.a = 10
    }
}
   var fn2 = new fn();//1.从fn方法里面new出一个实例,2.生成对象
    fn2.__proto__.age = 20;//把fn中的age属性改成20
console.log(typeof fn2)//object
console.log(fn2.__proto__ === fn.prototype)//true
console.log(fn2.__proto__)//结果:age:20 name:'haha' xixi:function() constructor __proto__
console.log(fn.prototype)//结果:age:20 name:'haha' xixi:function() constructor __proto__

this关键字