当前位置: 移动技术网 > IT编程>开发语言>JavaScript > javascript创建对象、对象继承的实用方式详解

javascript创建对象、对象继承的实用方式详解

2019年07月31日  | 移动技术网IT编程  | 我要评论

本文约定:不特殊声明的情况下,属性代指属性或方法。

创建对象、对象继承实际上是一回事:我们所需要的实例对象通过构造函数获得私有属性、通过原型链获得共享的属性。什么是好的方式?私有属性通过构造函数的方式获得(不考虑实例中自定义私有属性)且不需要重写,共享属性通过原型链找到且不需要重复创建。

普适的方式

组合使用构造函数模式和原型模式创建对象

function hnu_student(name) {
  this.name = name;
  this.sayname = function() {
    return this.name;
  };
}
hnu_student.prototype = {
  school: 'hnu',
  sayschool: function() {
    return this.school;
  }
};
object.defineproperty(hnu_student, 'constructor', {value: hnu_student});

var hiyohoo = new hnu_student('xujian');

通过字面量的方式会重写prototype,且原型的constructor指向了object,必要的情况下需要重新定义constructor。

寄生组合式继承

function object(o) {
  function f() {};
  f.prototype = o;
  return new f();
}
function inheritprototype(child, parent) {
  var prototype = object(parent.prototype);
  prototype.constructor = child;
  child.prototype = prototype;
}

function hnu_student(name) {
  this.name = name;
  this.sayname = function() {
    return this.name;
  };
}
hnu_student.prototype.school = 'hnu';
hnu_student.prototype.sayschool = function() {
  return this.school;
};

function student_2011(name, number) {
  hnu_student.call(this, name);
  this.number = number;
  this.saynumber = function() {
    return this.number;
  }
}
inheritprototype(student_2011, hnu_student);
student_2011.prototype.graduationtime = 2015;
student_2011.prototype.saygraduationtime = function() {
  return this.graduationtime;
};

var hiyohoo = new student_2011('xujian', 20110803203);

object()的作用:将作为参数传入的对象变成实例的原型,该对象的属性被所有实例共享。

共享属性:inheritprototype(student_2011, hnu_student);,子构造函数原型成为超构造函数原型的一个实例,超构造函数原型中的属性共享给子构造函数。
私有属性:hnu_student.call(this, name);,通过子构造函数创建实例时调用超构造函数创建私有属性。

创建对象的其他方式

动态原型模式

function hnu_student(name) {
  this.name = name;
  this.sayname = function() {
    return this.name;
  };

  if (!hnu_student.prototype.school) {
    hnu_student.prototype.school = 'hnu';
    hnu_student.prototype.sayschool = function() {
      return this.school;
    };
  }
}

var hiyohoo = new hnu_student('xujian');

将定义在原型中的共享属性放入构造函数中,使用判断语句,在第一次调用构造函数创建实例时,初始化原型共享属性。

寄生构造函数模式

function specialarray() {
  var values = new array();
  values.push.apply(values, arguments);
  values.topipedstring = function() {
    return this.join('|');
  };

  return values;
}

var colors = new specialarray('red', 'black', 'white');

用于为原生构造函数添加特殊的属性。

对象继承的其他方式

组合继承

function hnu_student(name) {
  this.name = name;
  this.sayname = function() {
    return this.name;
  };
}
hnu_student.prototype.school = 'hnu';
hnu_student.prototype.sayschool = function() {
  return this.school;
};
function student_2011(name, number) {
  hnu_student.call(this, name);
  this.number = number;
  this.saynumber = function() {
    return this.number;
  };
}
student_2011.prototype = new hnu_student();
student_2011.prototype.constructor = student_2011;
student_2011.prototype.graduationtime = 2015;
student_2011.prototype.saygraduationtime = function() {
  return this.graduationtime;
}
var hiyohoo = new student_2011('xujian', 20110803203);

共享属性:student_2011.prototype = new hnu_student();,子构造函数的原型就指向了超构造函数的原型,实例通过原型链找到所有共享的属性。
私有属性:hnu_student.call(this, name);,通过子构造函数创建实例时调用超构造函数创建私有属性。

缺陷:超构造函数被调用了两遍。student_2011.prototype = new hnu_student();的同时,在子构造函数原型中创建了超构造函数定义的私有属性,这些原型中的私有属性被实例中的同名属性覆盖屏蔽。

原型式继承、寄生式继承

function object(o) {
  function f() {}
  f.prototype = o;
  return new f();
}
var student1 = {
  school: 'hnu',
  sayschool: function() {
    return this.school;
  }
};
var student2 = object(student1);

object.creat()是ecmascript5新增的方法,接受两个参数:一是作为原型的原对象,二是重写或新增属性的对象,作用与自定义的object()相同。

var student1 = {
  name: 'xujian',
  school: 'hnu'
};
var student2 = object.create(student1, {
  name: {
    value: 'huangjing'
  }
});

寄生式继承在原型式继承的基础上添加了额外的属性用来增强对象。

function object(o) {
  function f() {}
  f.prototype = o;
  return new f();
}
function creatanother(original) {
  var clone = object(original);
  clone.sayhi = function() {
    alert('hi!');
  };
  return clone;
}
var student1 = {
  school: 'hnu',
  sayschool: function() {
    return this.school;
  }
};
var student2 = creatanother(student1);

原型式继承和寄生式继承用于创建与已有对象类似的实例对象。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网