3

In Javascript, Object has an assign function. Since an object literal has a __proto__ of Object.prototype, why can't an object literal use assign and must go through Object directly?

Object.assign({}, {hello: 'world'})

const o = {};
o.assign({add: 'more stuff'})
3
  • Not sure what you're asking. your first piece of code works fine; it will assign the properties and values of the second object to the first object. You're not doing anything with the result, but it's doing what you ask it to... Commented Nov 29, 2018 at 21:45
  • Probably because assign is a static method from the Object class. Why would you want to tho anyway? You can always just say o.add = 'more stuff'; Commented Nov 29, 2018 at 21:45
  • @IsaacVidrine I don't want to do it. Wrapping my head around inheritance in JS, thus I asked the question Commented Nov 29, 2018 at 21:53

3 Answers 3

3

The Object prototype does not have the method assign on it. Open the F12 console and type Object.prototype to see what is available on the base object prototype.

Object.assign is considered a static method, not an instance method.

Sign up to request clarification or add additional context in comments.

Comments

1

It's because assign is sort of a static function. It is not defined into the prototype.

Here is an example:

function SomeClass(foo) {
  this.foo = foo;
}

// this is a method, it is attached to the prototype
SomeClass.prototype.showFoo = function() {
  console.log(this.foo);
}

// this is a static method, it is not attached to the prototype
SomeClass.bar = function() {
  console.log("bar");
}

var instance = new SomeClass("foo");

console.log(instance.showFoo);       // OK
console.log(instance.bar);           // not OK

console.log(SomeClass.showFoo);      // not OK
console.log(SomeClass.bar);          // OK

5 Comments

Not sort of, it is.
@IsaacVidrine a static function would require a static keyword. Javascript doesn't have static functions, it just mimics them.
Well using that logic, saying var k = 1 means that the data type of k isn't the actually int, it just mimics int
@IsaacVidrine Yes, var k = 1 just mimics int because javascript is an untyped language. It only has one type for numeric values called number.
Thanks, learned something. Wasn't aware of the concept of "first class" objects.
0

The assign function isn't in the prototype property of Object and just the properties and methods defined on it can be accessed by created objects, to prove that let's do this:

Object.prototype.assign = function(){}; // this create a function called assign in the prototype property of Object
var obj = {};
obj.assign(); // Now we can access the assign function with the object literal

Hope this help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.