1.

 // Below is an example of how to use Object.create() to achieve classical inheritance. This is for single inheritance, which is all that JavaScript supports.
// Shape - superclass
function Shape() {
this.x = 0;
this.y = 0;
} // superclass method
Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
console.info('Shape moved.');
}; // Rectangle - subclass
function Rectangle() {
Shape.call(this); // call super constructor.
} // subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle; var rect = new Rectangle(); console.log('Is rect an instance of Rectangle?', rect instanceof Rectangle);// true
console.log('Is rect an instance of Shape?', rect instanceof Shape);// true
rect.move(1, 1); // Outputs, 'Shape moved.' // If you wish to inherit from multiple objects, then mixins are a possibility.
// The mixin function would copy the functions from the superclass prototype to the subclass prototype, the mixin function needs to be supplied by the user. An example of a mixin like function would be jQuery.extend(). function MyClass() {
SuperClass.call(this);
OtherSuperClass.call(this);
} MyClass.prototype = Object.create(SuperClass.prototype); // inherit
mixin(MyClass.prototype, OtherSuperClass.prototype); // mixin MyClass.prototype.myMethod = function() {
// do a thing
}; // Using propertiesObject argument with Object.create()
var o; // create an object with null as prototype
o = Object.create(null); o = {};
// is equivalent to:
o = Object.create(Object.prototype); // Example where we create an object with a couple of sample properties.
// (Note that the second parameter maps keys to *property descriptors*.)
o = Object.create(Object.prototype, {
// foo is a regular 'value property'
foo: { writable: true, configurable: true, value: 'hello' },
// bar is a getter-and-setter (accessor) property
bar: {
configurable: false,
get: function() { return 10; },
set: function(value) { console.log('Setting `o.bar` to', value); }
/* with ES5 Accessors our code can look like this
get function() { return 10; },
set function(value) { console.log('setting `o.bar` to', value); } */
}
}); function Constructor() {}
o = new Constructor();
// is equivalent to:
o = Object.create(Constructor.prototype);
// Of course, if there is actual initialization code in the
// Constructor function, the Object.create() cannot reflect it // Create a new object whose prototype is a new, empty object
// and add a single property 'p', with value 42.
o = Object.create({}, { p: { value: 42 } }); // by default properties ARE NOT writable, enumerable or configurable:
o.p = 24;
o.p;
// o.q = 12;
for (var prop in o) {
console.log(prop);
}
// 'q' delete o.p;
// false // to specify an ES3 property
o2 = Object.create({}, {
p: {
value: 42,
writable: true,
enumerable: true,
configurable: true
}
}); // Polyfill
// This polyfill covers the main use case which is creating a new object for which the prototype has been chosen but doesn't take the second argument into account. // Note that while the setting of null as [[Prototype]] is supported in the real ES5 Object.create, this polyfill cannot support it due to a limitation inherent in versions of ECMAScript lower than 5.
if (typeof Object.create != 'function') {
Object.create = (function() {
var Temp = function() {};
return function (prototype) {
if (arguments.length > 1) {
throw Error('Second argument not supported');
}
if(prototype !== Object(prototype) && prototype !== null) {
throw TypeError('Argument must be an object or null');
}
if (prototype === null) {
throw Error('null [[Prototype]] not supported');
}
Temp.prototype = prototype;
var result = new Temp();
Temp.prototype = null;
return result;
};
})();
}

最新文章

  1. C#设计模式系列:简单工厂模式(Simple Factory)
  2. WebLogic集群体系架构
  3. 24. Swap Nodes in Pairs
  4. AngularJS 开发中常犯的10个错误
  5. jquery中attr和prop的区别(转)
  6. 802.1x协议&eap类型
  7. fetch the words from url
  8. Android服务之Service
  9. 关于HTML与CSS编写规范
  10. n & (n-1)
  11. html-----006
  12. JVM-ClassLoader(转)
  13. 学习:WordXML格式初步分析
  14. mustache.js使用基本(三)
  15. Crash CodeForces - 417B
  16. C#标识符与关键字
  17. 【Spark篇】---Spark初始
  18. python内置函数 和模块函数总结
  19. h5网页水印SDK的实现代码示例
  20. Linux安装和设置Samba服务器

热门文章

  1. SharePoint2013 Online中InfoPath 无法调用WebService
  2. SharePoint2010基于表单验证方法总结(转载)
  3. C++ 命名空间解释
  4. 020:Buffer Pool 、压缩页、CheckPoint、Double Write、Change Buffer
  5. python复习之路-Day01
  6. Jquery 页面初始化常用的三种方法以及Jquery 发送ajax 请求
  7. 解决 mysql 数据库 挂掉了
  8. Python实践练习:strip()的正则表达式版本
  9. leetcode442
  10. 「小程序JAVA实战」小程序模块之间引用(19)