0

I was going through the videos from Crockford and found out Object() linkage topic.

I did not get the exact difference between Object() linkage and assignment though.

Can someone put some light to clear this up.

Below is the snippet code and result when executed in Chrome Browser

var myObj= {name:"Jack", age:25};

var myLinkObj= Object(myObj);

var myRef=myObj;


myLinkObj.name="John";
myRef.add="India";


alert(myObj.name);  // output John
alert(myObj.add); //output "India"

So what is the extra feature that I get form Object(). I find it is similar to value reference.

4
  • There is absolutely zero point in using Object() here, myObj === myLinkObj. What part of which video do you refer to? Commented Aug 26, 2014 at 11:40
  • I found in Douglas Crockford: "The JavaScript Programming Language"/2 of 4 video during Linkage explanation. Eventhough example I used is different from what he has shown. Commented Aug 26, 2014 at 11:51
  • A link would be nice I meant... Commented Aug 26, 2014 at 11:52
  • yui.zenfs.com/theater/crockford-tjpl-2.m4v Linkage topic starts at around 16:00 min Commented Aug 27, 2014 at 3:11

2 Answers 2

2

So what is the extra feature that I get form Object(). I find it is similar to value reference.

That's true. The Object function does simply return the reference to the given object.

However, that's not what Crockford is talking about in his video. He is trying to explain prototype inheritance, and he does not use the Object function for that but his object function that he only presents on his website and does not mention this in the talk (maybe another, earlier video?). It is defined as following:

function object(o) {
    function F() {}
    F.prototype = o;
    return new F();
}

However, that website is a bit outdated, and the language has evolved. Since EcmaScript 5 we have a native function with this functionality in the language: Object.create. You'd just do

var object = Object.create;
Sign up to request clarification or add additional context in comments.

Comments

0
var myObj= {name:"Jack", age:25};

var myLinkObj= Object.create(myObj);

myLinkObj.name="John";
myLinkObj.add="India";


alert(myLinkObj.name); //John
alert(myLinkObj.age); //25
alert(myLinkObj.add);//India
alert(myObj.name);//Jack

This works as prototype inheritance concept. myLinkobj inherit the the properties from myObj in this case. Thanks, Brinal

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.