object-create

How to prevent changes to a prototype?

馋奶兔 提交于 2020-02-23 10:41:32
问题 In this code, the prototype can still change. How I can prevent changes to the prototype? var a = {a:1} var b={b:1} var c = Object.create(a) Object.getPrototypeOf(c) //a c.__proto__ = b; Object.getPrototypeOf(c) //b var d = Object.create(null) Object.getPrototypeOf(d) //null d.__proto__ = b; Object.getPrototypeOf(d) //null 回答1: How I can prevent changes to the prototype? I assume you are not talking about mutating the prototype object itself, but overwriting the prototype of an existing

Understanding Crockford's Object.create shim

女生的网名这么多〃 提交于 2020-01-07 03:55:10
问题 I've been reading up on the Crockford shim for preventing the overwriting of prototypes, and understand that it's not the end-all/be-all solution at times. I also understand that ES5 Shim may be a viable alternative to this. I also read this post which provides a more robust, secure alternative. Still, I'd like to know what his Object.create shim is "saying" and then "doing." Can someone please tell me if my explanation comments are right? if (typeof Object.create === 'undefined') { //If the

Object.create method in javascript

馋奶兔 提交于 2019-12-19 18:10:09
问题 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

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

ぃ、小莉子 提交于 2019-12-19 05:56:52
问题 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 ? 回答1: So far, if you want to create an object, you can only use literals: var obj = {}; or the Object

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

余生颓废 提交于 2019-12-17 23:22:59
问题 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? 回答1: 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]],

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

北城以北 提交于 2019-12-09 05:16:32
问题 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

From which version, IE can support Object.create(null)?

跟風遠走 提交于 2019-12-07 14:47:36
问题 You can create an object in JavaScript in many ways: // creates an object which makes the Object, prototype of data. var data1 = new Object(); // Object literal notation; Object still is the prototype of data2. var data2 = {}; // anotherObject is now the prototype of data3. var data3 = Object.create(anotherObject); /* data3 is an object which can be verified bye typeof operator, however, it now has no prototype and you can build everything from scratch. */ var data3 = Object.create(null); But

From which version, IE can support Object.create(null)?

北城余情 提交于 2019-12-06 02:43:48
You can create an object in JavaScript in many ways: // creates an object which makes the Object, prototype of data. var data1 = new Object(); // Object literal notation; Object still is the prototype of data2. var data2 = {}; // anotherObject is now the prototype of data3. var data3 = Object.create(anotherObject); /* data3 is an object which can be verified bye typeof operator, however, it now has no prototype and you can build everything from scratch. */ var data3 = Object.create(null); But I don't know which versions of IE support the last method, i.e. Object.create(null) method? Matías

Advantage of using Object.create

こ雲淡風輕ζ 提交于 2019-12-04 18:34:16
问题 Similar to, but different from this question. The code below is from JavaScript: The Definitive Guide. He's basically defining an inherit method that defers to Object.create if it exists, otherwise doing plain old Javascript inheritance using constructors and swapping prototypes around. My question is, since Object.create doesn't exist on plenty of common browsers IE, what's the point of even trying to use it? It certainly clutters up the code, and one of the commenters on the previous

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

孤街浪徒 提交于 2019-12-04 06:05:05
问题 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