Object.create 方法

Object.create(proto[, propertiesObject])
The Object.create() method creates a new object with the specified prototype object and properties.
第1个参数是该对象的 prototype, 第2个参数和 Object.defineProperties 第2个参数类似

var o;

// create an object with null as prototype
o = Object.create(null);

o = {};
// is equivalent to:
o = Object.create(Object.prototype);

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

// 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);
    } */
  }
});

// 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;
// 42

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

if (typeof Object.create != ‘function‘) {
  Object.create = (function(undefined) {
    var Temp = function() {};
    return function (prototype, propertiesObject) {
      if(prototype !== Object(prototype)) {
        throw TypeError(
          ‘Argument must be an object, or null‘
        );
      }
      Temp.prototype = prototype || {};
      var result = new Temp();
      Temp.prototype = null;
      if (propertiesObject !== undefined) {
        Object.defineProperties(result, propertiesObject);
      } 

      // to imitate the case of Object.create(null)
      if(prototype === null) {
         result.__proto__ = null;
      }
      return result;
    };
  })();
}

参考地址:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create

时间: 2024-12-11 06:52:49

Object.create 方法的相关文章

javascript:利用Object.create()方法创建对象

在javascript里面,我们知道有两种常见的创建对象的方法,一种是使用对象直接量: 对象直接量是由若干值/键对组成的映射表,值/键对用逗号”,“分隔开,整个部分用花括号”{}“括起来. 例如: var empty={}; var people = { name:'kobe', age:'34' }; 另外一种方法是通过new创建对象:在new后面使用一个函数调用,这个函数也成为构造函数,通过new来初始化一个新的对象. 例如: var arr = new Array(); var date

Object.create()方法的低版本兼容问题

Object.prototype.create = function(obj) { if(Object.prototype.create){ return Object.prototype.create //特性检测,判断浏览器是否兼容 }else{ function F(){ F.prototype = obj; //以传入参数为原型构造对象 return new F(); } } };

firefox-Developer开发者站点——关于Object.create()新方法的介绍

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create Object.create() 方法创建一个拥有指定原型和若干个指定属性的对象. 语法 Object.create(proto, [ propertiesObject ]) 参数 proto 一个对象,作为新创建对象的原型. propertiesObject 可选.该参数对象是一组属性与值,该对象的属性名称将

使用Object.create()实现继承

一.常见继承方式 我们日常开发中常见的继承方式主要有: 1.默认模式: Child.prototype = new Parent(); 2.借用构造函数: function Child(a, b, c, d) { Parent.apply(this, arguments); } 3.借用和设置原型: function Child(a, b, c, d) { Parent.apply(this, arguments); } Child.prototype = new Parent(); 4.共享原

Object.create()和new object()和{}的区别

Object.create()介绍Object.create(null) 创建的对象是一个空对象,在该对象上没有继承 Object.prototype 原型链上的属性或者方法,例如:toString(), hasOwnProperty()等方法 Object.create()方法接受两个参数:Object.create(obj,propertiesObject) ; obj:一个对象,应该是新创建的对象的原型. propertiesObject:可选.该参数对象是一组属性与值,该对象的属性名称将

instanceof,Object.getPrototypeOf(),Object.create(),Object.setPrototypeOf(),Object.prototype.isPrototypeOf(),Object.prototype.__proto__

一.instanceof instanceof运算符返回一个布尔值,表示指定对象是否为某个构造函数的实例 var v = new Vehicle(); v instanceof Vehicle // true instanceof对整个原型链上的对象都有效,因此同一个实例对象,可能会对多个构造函数都返回true var d = new Date(); d instanceof Date // true d instanceof Object // true 利用instanceof运算符,还可以

Object.create

Object.create() 方法使用指定的原型对象和其属性创建了一个新的对象. 语法 Object.create(proto, [ propertiesObject ]) 举个例子: function Constructor(){} o = new Constructor(); // 上面的一句就相当于: o = Object.create(Constructor.prototype); // 当然,如果在Constructor函数中有一些初始化代码,Object.create不能执行那些代

Object的方法

1.Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.它将返回目标对象. ES2015引入的 ,且可用polyfilled.要支持旧浏览器的话,可用使用jQuery.extend或者_.assign(). 2.Object.create() 方法会使用指定的原型对象及其属性去创建一个新的对象. 语法: Object.create(proto[, propertiesObject]) 实现类式继承 function MyClass() { SuperCl

js中的new操作符与Object.create()的作用与区别

js中的new操作符与Object.create()的作用与区别 https://blog.csdn.net/mht1829/article/details/76785231 2017年08月06日 19:19:26 阅读数:1058 一.new 操作符 JavaScript 中 new 的机制实际上和面向类的语言完全不同. 在 JavaScript 中,构造函数只是一些使用 new 操作符时被调用的函数.它们并不会属于某个类,也不会实例化一个类.实际上,它们甚至都不能说是一种特殊的函数类型,它