IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    JavaScript面向对象精要(二)

    dwqs发表于 2015-06-09 01:01:18
    love 0

    构造函数和原型对象

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

    1. var name = "Pomy";
    2. function Per(){
    3. console.log("Hello "+this.name);
    4. }
    5. var per1 = new Per(); //"Hello undefined"
    6. var per2 = Per(); //"Hello Pomy"

    使用new时,会自动创建this对象,其类型为构造函数类型,指向对象实例;缺少new关键字,this指向全局对象。
    可以用instanceof来检测对象类型,同时每个对象在创建时都自动拥有一个constructor属性,指向其构造函数(字面量形式或Object构造函数创建的对象,指向Object,自定义构造函数创建的对象则指向它的构造函数)。

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

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

    1. function Person(){}
    2. Person.prototype.name="dwqs";
    3. Person.prototype.age=20;
    4. Person.prototype.sayName=function()
    5. {
    6. alert(this.name);
    7. };
    8. var per1 = new Person();
    9. per1.sayName(); //dwqs
    10. var per2 = new Person();
    11. per2.sayName(); //dwqs
    12. alert(per1.sayName == per2.sayName); //true

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

    1. alert(Person.prototype.isPrototypeOf(per2)); //true
    2. per1.blog = "www.ido321.com";
    3. alert(per1.hasOwnProperty("blog")); //true
    4. alert(Person.prototype.hasOwnProperty("blog")); //false
    5. alert(per1.hasOwnProperty("name")); //false
    6. alert(Person.prototype.hasOwnProperty("name")); //true

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

    1. function Hello(name){
    2. this.name = name;
    3. }
    4. //重写原型
    5. Hello.prototype = {
    6. sayHi:function(){
    7. console.log(this.name);
    8. }
    9. };
    10. var hi = new Hello("Pomy");
    11. console.log(hi instanceof Hello); //true
    12. console.log(hi.constructor === Hello); //false
    13. console.log(hi.constructor === Object); //true

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

    1. Hello.prototype = {
    2. constructor:Hello,
    3. sayHi:function(){
    4. console.log(this.name);
    5. }
    6. };
    7. console.log(hi.constructor === Hello); //true
    8. console.log(hi.constructor === Object); //false

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

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

    继承

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

    1. var book = {
    2. title:"这是书名";
    3. };
    4. //和下面的方式一样
    5. var book = Object.create(Object.prototype,{
    6. title:{
    7. configurable:true,
    8. enumerable:true,
    9. value:"这是书名",
    10. wratable:true
    11. }
    12. });

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

    1. var book1 = {
    2. title:"JS高级程序设计",
    3. getTitle:function(){
    4. console.log(this.title);
    5. }
    6. };
    7. var book2 = Object.create(book1,{
    8. title:{
    9. configurable:true,
    10. enumerable:true,
    11. value:"JS权威指南",
    12. wratable:true
    13. }
    14. });
    15. book1.getTitle(); //"JS高级程序设计"
    16. book2.getTitle(); //"JS权威指南"
    17. console.log(book1.hasOwnProperty("getTitle")); //true
    18. console.log(book1.isPrototypeOf("book2")); //false
    19. console.log(book2.hasOwnProperty("getTitle")); //false

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

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

    1. function Rect(length,width){
    2. this.length = length;
    3. this.width = width;
    4. }
    5. Rect.prototype.getArea = function(){
    6. return this.width * this.length;
    7. };
    8. Rect.prototype.toString = function(){
    9. return "[Rect"+this.length+"*"+this.width+"]";
    10. };
    11. function Square(size){
    12. this.length = size;
    13. this.width = size;
    14. }
    15. //修改prototype属性
    16. Square.prototype = new Rect();
    17. Square.prototype.constructor = Square;
    18. Square.prototype.toString = function(){
    19. return "[Square"+this.length+"*"+this.width+"]";
    20. };
    21. var rect = new Rect(5,10);
    22. var square = new Square(6);
    23. console.log(rect.getArea()); //50
    24. console.log(square.getArea()); //36

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

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

    相关文章:
    DOM笔记(十二):又谈原型对象
    DOM笔记(十三):JavaScript的继承方式

    淡忘~浅思猜你喜欢

    JavaScript面向对象精要(一)

    【译】JavaScript面试问题:事件委托和this

    【译】有趣的JavaScript原生数组函数

    对象的浅复制和深复制

    因一段JavaScript代码引发的闲扯
    无觅

    转载请注明:淡忘~浅思 » JavaScript面向对象精要(二)



沪ICP备19023445号-2号
友情链接