object-create

Why is Object.create so much slower than a constructor?

你说的曾经没有我的故事 提交于 2019-12-03 05:34:46
Background In a project I'm maintaining we make extensive use of null prototype objects as a poor man's alternative to (string key only) Maps, which are not natively supported in many older, pre-ES6 browsers. Basically, to create a null prototype object on the fly, one would use: var foo = Object.create(null); This guarantees that the new object has no inherited properties, such as "toString", "constructor", "__proto__" which are not desirable for this particular use case. Since this pattern appears multiple times in code, we came up with the idea of writing a constructor that would create

How does object.create work in JavaScript?

强颜欢笑 提交于 2019-12-03 03:12:08
问题 Tell me if I'm wrong: A prototype is a normal object. When an object inherits a prototype, it does not just copy the properties of the prototype, the object stores a reference to the prototype. In Firefox, I can do: var food = {fruit:"apple"}; var more_food = {vegetable:"celery"}; food.__proto__ = more_food; food.vegetable // celery food.fruit // apple I can use the __proto__ property to manually set the reference to the prototype object. I can also use Object.create : var food = {fruit:

Understanding the difference between Object.create() and new SomeFunction()

可紊 提交于 2019-12-02 10:20:29
I recently stumbled upon the Object.create() method in JavaScript, and am trying to deduce how it is different from creating a new instance of an object with new SomeFunction() , and when you would want to use one over the other. Consider the following example: var test = { val: 1, func: function() { return this.val; } }; var testA = Object.create(test); testA.val = 2; console.log(test.func()); // 1 console.log(testA.func()); // 2 console.log('other test'); var otherTest = function() { this.val = 1; this.func = function() { return this.val; }; }; var otherTestA = new otherTest(); var

Object.create method in javascript

你离开我真会死。 提交于 2019-12-01 17:03:11
Being a beginner in javascript, i tried to understand Object.create() method from here https://developer-new.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create In the example code, line 18. A accessor property is created with writable set to true. I also read that writable is only for data descriptors. Tried running, var 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: { writable: true, configurable: false, get: function() { return 10

Is there any reason to use Object.create() or new in JavaScript?

牧云@^-^@ 提交于 2019-12-01 03:58:16
I've been using the new keyword in JavaScript so far. I have been reading about Object.create and I wonder if I should use it instead. What I don't quite get is that I often need to run construction code, so I don't see how Object.create is going to work at all since it does not trigger any functions to run. Could anyone tell me, In which case should I use Object.create instead of new ? So far, if you want to create an object, you can only use literals: var obj = {}; or the Object constructor. var obj = Object(); But none of these methods let you specify the prototype of the created object.

Object.create Prototype Chains

六眼飞鱼酱① 提交于 2019-11-29 15:13:59
Initial Question Yesterday i read about ECMAScript 5 Object.create() And I wanted to start building prototype Chains in my Code with this method instead of setting the prototype and its constructor, I like that you can directly set writable configurable etc.. I tried it like this function printobject(msg, obj) { if (msg) { document.write("<b>" + msg + "</b><br>"); document.write("<hr><br>"); } for (var prop in obj) { if (obj.hasOwnProperty(prop)) { if (obj[prop].toString() !== "[object Object]") { document.write(prop + " : " + obj[prop] + "<br>"); } else { document.write("<b>" + prop + " : " +

JavaScript Object.create — inheriting nested properties

霸气de小男生 提交于 2019-11-28 19:41:53
I've come across a peculiarity with Douglas Crockfords Object.create method which I'm hoping someone might be able to explain: If I create an object - say 'person' - using object literal notation then use Object.create to create a new object - say 'anotherPerson' - which inherits the methods and properties from the initial 'person' object. If I then change the name values of the second object - 'anotherPerson' - it also changes the name value of the initial 'person' object. This only happens when the properties are nested, this code should give you an idea of what I mean: if (typeof Object

What is difference between creating object using Object.create() and Object.assign()?

江枫思渺然 提交于 2019-11-28 18:26:32
Considering following code: var obj1 = Object.create({}, {myProp: {value: 1}}); var obj2 = Object.assign({}, {myProp: 1}); Is there any difference between obj1 and obj2 since each object has been created in a different way? Oriol Let's compare obj1 and obj2 in this code: var target1 = {}, target2 = {}; var obj1 = Object.create(target1, {myProp: {value: 1}}); var obj2 = Object.assign(target2, {myProp: 1}); Prototypical chain Object.create creates a new object with the specified [[Prototype]], and Object.assign assigns the properties directly on the specified object: obj1 !== target1; obj2 ===

JavaScript inheritance with Object.create()?

若如初见. 提交于 2019-11-28 05:30:45
How do I inherit with the Object.create()? I tried these, but none are working: var B = function() {}; var A = function() {}; A = Object.create(B); A.prototype.C = function() {}; and var B = function() {}; var A = function() {}; A.prototype.C = function() {}; A = Object.create(B); and var B = function() {}; A = Object.create(B); var A = function() {}; A.prototype.C = function() {}; Nothing worked. How am I supposed to use this new Object.create()-function? Object.create() is used to inherit objects, not constructors like you're trying to do. It pretty much creates a new object with the old

Javascript Arrays created with Object.create - not real Arrays?

天涯浪子 提交于 2019-11-27 22:26:53
It looks like Arrays created with Object.create walk like Arrays and quack like Arrays, but are still not real arrays. At least with v8 / node.js. > a = [] [] > b = Object.create(Array.prototype) {} > a.constructor [Function: Array] > b.constructor [Function: Array] > a.__proto__ [] > b.__proto__ [] > a instanceof Array true > b instanceof Array true > Object.prototype.toString.call(a) '[object Array]' > Object.prototype.toString.call(b) '[object Object]' Can some Javascript guru explain why it is so, and how to make it so that my newly created array is indistinguishable from a real array? My