继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

JavaScript面向对象精要(二)

jeck猫
关注TA
已关注
手记 298
粉丝 74
获赞 402

原文链接

JavaScript 面向对象精要(一)

构造函数和原型对象

构造函数也是函数,用new创建对象时调用的函数,与普通函数的一个区别是,其首字母应该大写。但如果将构造函数当作普通函数调用(缺少new关键字),则应该注意this指向的问题。

var name = "Pomy";function Per(){    console.log("Hello "+this.name);
}var per1 = new Per();  //"Hello undefined"var per2 = Per();   //"Hello Pomy"

使用new时,会自动创建this对象,其类型为构造函数类型,指向对象实例;缺少new关键字,this指向全局对象。

可以用instanceof来检测对象类型,同时每个对象在创建时都自动拥有一个constructor属性,指向其构造函数(字面量形式或Object构造函数创建的对象,指向Object,自定义构造函数创建的对象则指向它的构造函数)。

console.log(per1 instanceof Per);  //trueconsole.log(per1.constructor === Per); //true

每个对象实例都有一个内部属性:[[Prototype]],其指向该对象的原型对象。构造函数本身也具有prototype 属性指向原型对象。所有创建的对象都共享该原型对象的属性和方法。

function Person(){}
Person.prototype.name="dwqs";
Person.prototype.age=20;
Person.prototype.sayName=function(){
    alert(this.name);
};var per1 = new Person();
per1.sayName();  //dwqsvar per2 = new Person();
per2.sayName();  //dwqsalert(per1.sayName == per2.sayName);  //true

所以,实例中的指针仅指向原型,而不指向构造函数。 ES5提供了hasOwnProperty()和isPropertyOf()方法来反应原型对象和实例之间的关系

alert(Person.prototype.isPrototypeOf(per2));  //trueper1.blog = "www.ido321.com";
alert(per1.hasOwnProperty("blog"));  //truealert(Person.prototype.hasOwnProperty("blog"));  //falsealert(per1.hasOwnProperty("name"));  //falsealert(Person.prototype.hasOwnProperty("name"));  //true

因为原型对象的constructor属性是指向构造函数本身,所以在重写原型时,需要注意constructor属性的指向问题。

function Hello(name){    this.name = name;
}//重写原型Hello.prototype = {
    sayHi:function(){        console.log(this.name);
    }
};var hi = new Hello("Pomy");console.log(hi instanceof Hello);  //trueconsole.log(hi.constructor === Hello); //falseconsole.log(hi.constructor === Object); //true

使用对象字面量形式改写原型对象改变了构造函数的属性,因此constructor指向Object,而不是Hello。如果constructor指向很重要,则需要在改写原型对象时手动重置其constructor属性

Hello.prototype = {    constructor:Hello,
    sayHi:function(){        console.log(this.name);
    }
};console.log(hi.constructor === Hello); //trueconsole.log(hi.constructor === Object); //false

利用原型对象的特性,我们可以很方便的在JavaScript的内建原型对象上添加自定义方法:

Array.prototype.sum=function(){    return this.reduce(function(prev,cur){        return prev+cur;
    });
};var num = [1,2,3,4,5,6];var res = num.sum();console.log(res);  //21String.prototype.capit = function(){    return this.charAt(0).toUpperCase()+this.substring(1);
};var msg = "hello world";console.log(msg.capit()); //"Hello World"

继承

利用[[Prototype]]特性,可以实现原型继承;对于字面量形式的对象,会隐式指定Object.prototype为其[[Prototype]],也可以通过Object.create()显示指定,其接受两个参数:第一个是[[Prototype]]指向的对象(原型对象),第二个是可选的属性描述符对象。

var book = {
    title:"这是书名";
};//和下面的方式一样var book = Object.create(Object.prototype,{
    title:{
        configurable:true,
        enumerable:true,
        value:"这是书名",
        wratable:true
    }
});

字面量对象会默认继承自Object,更有趣的用法是,在自定义对象之间实现继承。

var book1 = {
    title:"JS高级程序设计",
    getTitle:function(){        console.log(this.title);
    }
};var book2 = Object.create(book1,{
    title:{
        configurable:true,
        enumerable:true,
        value:"JS权威指南",
        wratable:true
    }
});
book1.getTitle();  //"JS高级程序设计"book2.getTitle();  //"JS权威指南"console.log(book1.hasOwnProperty("getTitle"));  //trueconsole.log(book1.isPrototypeOf("book2"));  //falseconsole.log(book2.hasOwnProperty("getTitle"));  //false

当访问book2的getTitle属性时,JavaScript引擎会执行一个搜索过程:现在book2的自有属性中寻找,找到则使用,若没有找到,则搜索[[Prototype]],若没有找到,则继续搜索原型对象的[[Prototype]],直到继承链末端。末端通常是Object.prototype,其[[Prototype]]被设置为null。

实现继承的另外一种方式是利用构造函数。每个函数都具有可写的prototype属性,默认被自懂设置为继承自Object.prototype,可以通过改写它来改变原型链。

function Rect(length,width){    this.length = length;    this.width = width;
}
Rect.prototype.getArea = function(){    return this.width * this.length;
};
Rect.prototype.toString = function(){    return "[Rect"+this.length+"*"+this.width+"]";
};function Square(size){    this.length = size;    this.width = size;
}//修改prototype属性Square.prototype = new Rect();
Square.prototype.constructor = Square;
Square.prototype.toString = function(){    return "[Square"+this.length+"*"+this.width+"]";
};var rect = new Rect(5,10);var square = new Square(6);console.log(rect.getArea());  //50console.log(square.getArea());  //36

如果要访问父类的toString(),可以这样做:

Square.prototype.toString = function(){    var text = Rect.prototype.toString.call(this);    return text.replace("Rect","Square");
}


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP