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…
Javascript 1.9.3 / ECMAScript 5 introduces Object.create, which Douglas Crockford amongst others has been advocating for a long time. How do I replace new in the code below with Object.create?
var UserA = function(nameParam) {
this.id =…
In JavaScript what is the difference between these two examples:
Prerequisite:
function SomeBaseClass(){
}
SomeBaseClass.prototype = {
doThis : function(){
},
doThat : function(){
}
}
Inheritance example A using…
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?
Consider such an object with a prototype chain:
var A = {};
var B = Object.create(A);
var C = Object.create(B);
How to check in runtime if C has A in its prototype chain?
instanceof doesn't fit as it's designed to work with constructor functions,…
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…
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…
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 =…
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…
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…
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]
>…
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…
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…
I have been playing around with Object.create in the EcmaScript 5 spec, and I am trying to create a multiple inheritance type structure.
Say I have a few functions: a, b, and c. With only dealing with prototypes, I can do this:
function a ()…
I'm coming to Javascript from a background in Python and Smalltalk, and I appreciate the linage of Self and Lisp in the language. With ECMAScript5, I wanted to try my hand at prototypal OO without the new operator.
Constraints:
optional new…