Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript add method to object

Tags:

javascript

Suppose I have a Foo object

How can I extend this object by adding a bar() method and also ensure that future instances of Foo have this method?

like image 613
Chin Avatar asked Nov 23 '12 00:11

Chin


People also ask

Can method be added to an object in JavaScript?

JavaScript methods are actions that can be performed on objects. A JavaScript method is a property containing a function definition. Methods are functions stored as object properties.

What is ADD method in JavaScript?

add() The add() method inserts a new element with a specified value in to a Set object, if there isn't an element with the same value already in the Set .

How do you use a method on an object?

You also use an object reference to invoke an object's method. You append the method's simple name to the object reference, with an intervening dot operator (.). Also, you provide, within enclosing parentheses, any arguments to the method. If the method does not require any arguments, use empty parentheses.

How do you add a property to an object?

One way is to add a property using the dot notation: obj. foo = 1; We added the foo property to the obj object above with value 1.


2 Answers

you need to add it to Foo's prototype:

function Foo(){} Foo.prototype.bar = function(){} var x = new Foo() x.bar() 
like image 164
VeXii Avatar answered Oct 13 '22 05:10

VeXii


This all depends on how you're creating Foo, and how you intend to use .bar().

First, are you using a constructor-function for your object?

var myFoo = new Foo(); 

If so, then you can extend the Foo function's prototype property with .bar, like so:

function Foo () { /*...*/ } Foo.prototype.bar = function () { /*...*/ };  var myFoo = new Foo(); myFoo.bar(); 

In this fashion, each instance of Foo now has access to the SAME instance of .bar.
To wit: .bar will have FULL access to this, but will have absolutely no access to variables within the constructor function:

function Foo () { var secret = 38; this.name = "Bob"; } Foo.prototype.bar = function () { console.log(secret); }; Foo.prototype.otherFunc = function () { console.log(this.name); };  var myFoo = new Foo(); myFoo.otherFunc(); // "Bob"; myFoo.bar(); // error -- `secret` is undefined...              // ...or a value of `secret` in a higher/global scope 

In another way, you could define a function to return any object (not this), with .bar created as a property of that object:

function giveMeObj () {     var private = 42,         privateBar = function () { console.log(private); },         public_interface = {             bar : privateBar         };      return public_interface; }  var myObj = giveMeObj(); myObj.bar(); // 42 

In this fashion, you have a function which creates new objects.
Each of those objects has a .bar function created for them.
Each .bar function has access, through what is called closure, to the "private" variables within the function that returned their particular object.
Each .bar still has access to this as well, as this, when you call the function like myObj.bar(); will always refer to myObj (public_interface, in my example Foo).

The downside to this format is that if you are going to create millions of these objects, that's also millions of copies of .bar, which will eat into memory.

You could also do this inside of a constructor function, setting this.bar = function () {}; inside of the constructor -- again, upside would be closure-access to private variables in the constructor and downside would be increased memory requirements.

So the first question is:
Do you expect your methods to have access to read/modify "private" data, which can't be accessed through the object itself (through this or myObj.X)?

and the second question is: Are you making enough of these objects so that memory is going to be a big concern, if you give them each their own personal function, instead of giving them one to share?

For example, if you gave every triangle and every texture their own .draw function in a high-end 3D game, that might be overkill, and it would likely affect framerate in such a delicate system...

If, however, you're looking to create 5 scrollbars per page, and you want each one to be able to set its position and keep track of if it's being dragged, without letting every other application have access to read/set those same things, then there's really no reason to be scared that 5 extra functions are going to kill your app, assuming that it might already be 10,000 lines long (or more).

like image 33
Norguard Avatar answered Oct 13 '22 06:10

Norguard