Jump to content

  • Log In with Google      Sign In   
  • Create Account

Making an object in Javascript?


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
6 replies to this topic

#1   GDNet+   

6811
Like
1Likes
Like

Posted 03 April 2014 - 11:42 AM

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, 03 April 2014 - 11:43 AM.

External Articulation of Concepts Materializes Innate Knowledge of One's Craft and Science
 
Beginner in Game Development? Read here. And read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#2   Moderators   

9749
Like
0Likes
Like

Posted 03 April 2014 - 12:08 PM

Depends on the paradigm you wish to use, OOP or data-driven. Mozilla has a detailed article on OOP programming in javascript which may answer some of your questions.
zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

#3   Members   

838
Like
0Likes
Like

Posted 03 April 2014 - 04:18 PM

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.



#4   Members   

4430
Like
4Likes
Like

Posted 03 April 2014 - 04:39 PM

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.

. David Gill :: GitHub :: twitter .


#5   Members   

4976
Like
0Likes
Like

Posted 07 April 2014 - 01:02 AM


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.


Previously "Krohm"


#6   GDNet+   

6811
Like
0Likes
Like

Posted 07 April 2014 - 06:55 AM

 


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, 07 April 2014 - 06:55 AM.

External Articulation of Concepts Materializes Innate Knowledge of One's Craft and Science
 
Beginner in Game Development? Read here. And read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#7   GDNet+   

2390
Like
0Likes
Like

Posted 07 April 2014 - 07:36 AM

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;




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.