深入分析js中的this、constructor 和prototype

this

this表示当前对象,如果在全局作用范围内使用this,则指代当前页面对象window; 如果在函数中使用this,则this指代运行时此函数在什么对象上被调用。 我们还可以使用apply和call两个全局方法来改变函数中this的具体指向

先看一个在全局作用范围内使用this的例子:

console.log(this === window); // true
console.log(window.alert === this.alert); // true
console.log(this.parseInt("021", 10)); // 21

函数中的this是在运行时决定的,而不是函数定义时,如下:

function foo() {   // 定义一个全局函数
    console.log(this.fruit);
}
var fruit = "apple";   // 定义一个全局变量,等价于window.fruit = "apple";此时函数foo中this指向window对象,这种调用方式和window.foo();是完全等价的    
foo(); // "apple"
var pack = {   // 自定义一个对象,并将此对象的属性foo指向全局函数foo
    fruit: "orange",
    foo: foo
};
pack.foo(); // "orange"  此时函数foo中this指向window.pack对象

全局函数apply和call可以用来改变函数中this的指向

function foo() {   // 定义一个全局函数
    console.log(this.fruit);
}
var fruit = "apple";   // 定义一个全局变量
var pack = {   // 自定义一个对象
    fruit: "orange"
};
foo.apply(window);    // "apple"   等价于window.foo();
foo.apply(pack);  // "orange"   此时foo中的this === pack

注:apply和call两个函数的作用相同,唯一的区别是两个函数的参数定义不同

因为在JavaScript中函数也是对象,所以我们可以看到如下有趣的例子:

function foo() {   // 定义一个全局函数
    if (this === window) {
        console.log("this is window.");
    }
}
foo.boo = function() {   // 函数foo也是对象,所以可以定义foo的属性boo为一个函数
    if (this === foo) {
        console.log("this is foo.");
    } else if (this === window) {
        console.log("this is window.");
    }
};
foo(); // this is window.   等价于window.foo();
foo.boo(); // this is foo.   可以看到函数中this的指向调用函数的对象
foo.boo.apply(window); // this is window.   使用apply改变函数中this的指向

prototype

prototype本质上还是一个JavaScript对象。并且每个函数都有一个默认的prototype属性。 如果这个函数被用在创建自定义对象的场景中,我们称这个函数为构造函数

只有构造函数才有prototype属性,而构造函数的实例是没有该属性的。在javascript中,每个函数都自动有一个prototype属性,而不是每一个对象拥有prototype属性

原型属性与实例对象的创建与否没有关系,它在对象创建之前就已经存在

function Person(name) {   // 构造函数
    this.name = name;
}
Person.prototype = {   // 定义Person的原型,原型中的属性可以被自定义对象引用
    getName: function() {
        return this.name;
    }
}
var hao= new Person("haorooms");
console.log(hao.getName());     // "haorooms"

作为类比,我们考虑下JavaScript中的数据类型 - 字符串(String)、数字(Number)、数组(Array)、对象(Object)、日期(Date)等。我们有理由相信,在JavaScript内部这些类型都是作为构造函数来实现的

function Array() {   // 定义数组的构造函数,作为JavaScript的一种预定义类型
    // ...
}     
var arr1 = new Array(1, 56, 34, 12);   // 初始化数组的实例  
// 但是,我们更倾向于如下的语法定义:  
var arr2 = [1, 56, 34, 12];   

同时对数组操作的很多方法(比如concat、join、push)应该也是在prototype属性中定义的。 实际上,JavaScript所有的固有数据类型都具有只读的prototype属性 (这是可以理解的:因为如果修改了这些类型的prototype属性,则那些预定义的方法就消失了), 但是我们可以向其中添加自己的扩展方法

Array.prototype.min = function() {   //向JS固有类型Array扩展一个获取最小值的方法
    var min = this[0];
    for (var i = 1; i < this.length; i++) {
        if (this[i] < min) {
            min = this[i];
        }
    }
    return min;
};
console.log([1, 56, 34, 12].min()); // 1   // 在任意Array的实例上调用min方法

function定义的对象有一个prototype属性,使用new生成的对象就没有这个prototype属性

prototype属性又指向了一个prototype对象,注意prototype属性与prototype对象是两个不同的东西,要注意区别。在prototype对象中又有一个constructor属性,这个constructor属性同样指向一个constructor对象,而这个constructor对象恰恰就是这个function函数本身

function Person(name){   
    this.name=name;   
    this.showMe=function(){ 
        alert(this.name); 
    }   
};    
var one=new Person('js');
alert(one.prototype)   //undefined   
alert(typeof Person.prototype);   //object   
alert(Person.prototype.constructor);   //function Person(name) {...};  

上面的代码证明了one这个对象没有prototype属性

function Person(name){   
    this.name=name;   
    this.showMe=function(){   
        alert(this.name);   
    }   
}; 
Person.prototype.from=function(){   
    alert('I come from prototype.');   
}
var one=new Person('js'); 
one.showMe();   //js,这个结果没有什么好奇怪的   
one.from();   //I come from prototype.,这个结果有一点奇怪吧  
alert(one.constructor);   //function Person(name) {...}   
alert(Person.prototype.constructor);   //function Person(name) {...}  

要解释这个结果就要仔细研究一下new这个操作符了.var one=new Person('js');这个语句执行的过程可以分成下面的语句:

var one={};   
Person.call(one,'js'); 

new形式创建对象的过程实际上可以分为三步:

第一步是建立一个新对象(叫A吧)

第二步将该对象(A)内置的原型对象设置为构造函数(就是Person)prototype 属性引用的那个原型对象

第三步就是将该对象(A)作为this 参数调用构造函数(就是Person),完成成员设置等初始化工作

其中第二步中出现了一个新名词就是内置的原型对象,注意这个新名词跟prototype对象不是一回事,为了区别我叫它inobj,inobj就指向了函数Person的prototype对象。在person的prototype对象中出现的任何属性或者函数都可以在one对象中直接使用,这个就是javascript中的原型继承了

这样one对象通过内置的原型对象inobj就可以直接访问Person的prototype对象中的任何属性与方法了。这也就解释了上面的代码中 为什么one可以访问form函数了。因为prototype对象中有一个constructor属性,那么one也可以直接访问constructor 属性

constructor

constructor始终指向创建当前对象的构造函数,返回对创建此对象的数组函数的引用

语法

object.constructor

举例:

var arr = [1, 56, 34, 12];   // 等价于 var foo = new Array(1, 56, 34, 12);
console.log(arr.constructor === Array);    // true
var Foo = function() { };   // 等价于 var foo = new Function();
console.log(Foo.constructor === Function); // true
var obj = new Foo();   // 由构造函数实例化一个obj对象
console.log(obj.constructor === Foo);   // true
console.log(obj.constructor.constructor === Function); // true   // 将上面两段代码合起来,就得到下面的结论

但是当constructor遇到prototype时,有趣的事情就发生了。 我们知道每个函数都有一个默认的属性prototype,而这个prototype的constructor默认指向这个函数

function Person(name) {
    this.name = name;
};
Person.prototype.getName = function() {
    return this.name;
};
var p = new Person("haorooms");
console.log(p.constructor === Person); // true
console.log(Person.prototype.constructor === Person); // true
// 将上两行代码合并就得到如下结果
console.log(p.constructor.prototype.constructor === Person); // true

当时当我们重新定义函数的prototype时(注意:和上例的区别,这里不是修改而是覆盖), constructor的行为就有点奇怪了

function Person(name) {
    this.name = name;
};
Person.prototype = {
    getName: function() {
        return this.name;
    }
};
var p = new Person("haorooms");
console.log(p.constructor === Person); // false
console.log(Person.prototype.constructor === Person); // false
console.log(p.constructor.prototype.constructor === Person); // false

为什么呢? 原来是因为覆盖Person.prototype时,等价于进行如下代码操作:

Person.prototype = new Object({
    getName: function() {
        return this.name;
    }
});

而constructor始终指向创建自身的构造函数,所以此时Person.prototype.constructor === Object,即是:

function Person(name) {
    this.name = name;
};
Person.prototype = {
    getName: function() {
        return this.name;
    }
};
var p = new Person("haorooms");
console.log(p.constructor === Object); // true
console.log(Person.prototype.constructor === Object); // true
console.log(p.constructor.prototype.constructor === Object); // true

怎么修正这种问题呢?方法也很简单,重新覆盖Person.prototype.constructor即可:

function Person(name) {
    this.name = name;
};
Person.prototype = {
    getName: function() {
        return this.name;
    }
};
Person.prototype.constructor = Person;
var p = new Person("haorooms");
console.log(p.constructor === Person); // true
console.log(Person.prototype.constructor === Person); // true
console.log(p.constructor.prototype.constructor === Person); // true

也可以这么写:

function Person(name) {
    this.name = name;
};
Person.prototype = {
    constructor:Person,//指定constructor
    getName: function() {
        return this.name;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值