Jump to content
  • Advertisement
Sign in to follow this  
Alpha_ProgDes

Making an object in Javascript?

This topic is 2019 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know you can do something like this:

var obj = {stuff: 'blah', work: function foo () {return 1}; place: 'bar'};

But you can also do this, I believe:

var obj = function () 
          { 
             this.stuff = 'blah', 
             this.work = function foo () {return 1}, 
             this.place = 'bar'
          };

So is there a difference between creating an object with a function as opposed to object notation? When would I use one over the other?

Edited by Alpha_ProgDes

Share this post


Link to post
Share on other sites
Advertisement

I'm not a big javascript guy, but I don't think it really matters in your specific example. I mean you are creating objects not classes. So I would think you could do it however you want. When it comes to creating "classes" then you have a couple choices.

Share this post


Link to post
Share on other sites
The main consideration would be that the literal object notation just creates a single object, whereas the function is a constructor from which you can instantiate many new independent objects, rather like a class in some other mainstream OO language.

Share this post


Link to post
Share on other sites


But you can also do this, I believe:

<snippet removed>

While this is valid, I'm not sure it is going to work as you expect as you need to provide the function an object to use as this. This task is normally carried out by new, but last time I tried (about two months ago) new would only allow ctors to be real first-level functions, not lambdas.

 

That is, calling new(obj) didn't work and thrown an exception.

Share this post


Link to post
Share on other sites

 


But you can also do this, I believe:

<snippet removed>

While this is valid, I'm not sure it is going to work as you expect as you need to provide the function an object to use as this. This task is normally carried out by new, but last time I tried (about two months ago) new would only allow ctors to be real first-level functions, not lambdas.

 

That is, calling new(obj) didn't work and thrown an exception.

 

 

Assume that I expect nothing and am wanting to learn the difference between the two :)

Edited by Alpha_ProgDes

Share this post


Link to post
Share on other sites
There are constructors in JavaScript, which is what your example demonstrates. Also using a function to define your object with a constructor will enable typeof and instanceof to return more specific result than just "object". You can reference the constructor with .prototype.constructor. Leveraging that property enables a sort of classical oop style inheritance to be implemented.

function Base(a, b) {}

function Derived (a, b) {
Base.prototype.constructor.call(this, a, b);
}
Derived.prototype = Object.create(Base.prototype);
Derived.prototype.constructor = Derived;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!