您的位置:首页 > Web前端 > JavaScript

JavaScript面向对象精要(二)

2015-06-24 14:33 706 查看

构造函数和原型对象

构造函数也是函数,用
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);  //true
console.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();  //dwqs
var per2 = new Person();
per2.sayName();  //dwqs
alert(per1.sayName == per2.sayName);  //true





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

alert(Person.prototype.isPrototypeOf(per2));  //true
per1.blog = "www.ido321.com";
alert(per1.hasOwnProperty("blog"));  //true
alert(Person.prototype.hasOwnProperty("blog"));  //false
alert(per1.hasOwnProperty("name"));  //false
alert(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);  //true
console.log(hi.constructor === Hello); //false
console.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); //true
console.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);  //21

String.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"));  //true
console.log(book1.isPropertyOf("book2"));  //true
console.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());  //50
console.log(square.getArea());  //36


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

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


相关文章:

DOM笔记(十二):又谈原型对象

DOM笔记(十三):JavaScript的继承方式

原文:http://www.ido321.com/1586.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: