How do you create a method for a custom object in JavaScript?

What is it like...

var obj = new Object(); obj.function1 = function(){ //code } 

or something like that?

+46
javascript object methods
Feb 02 '09 at 20:42
source share
7 answers
 var newObj = { met1 : function () { alert('hello'); } }; 

Then the method can be called like this:

 newObj.met1(); 

Btw, when declaring a new object, use the object literal ( {} ), not the new Object() constructor.

+47
Feb 02 '09 at 20:45
source share

You can see from the answers that you already have several ways.

 #1 var o = new Object(); o.method = function(){} #2 var o = new Object(); o.prototype.method = function(){} #3 function myObject() { this.method = function(){} } var o = new myObject(); #4 function myObject() {} myObject.prototype.method = function(){} var o = new myObject(); #5 var o = { method: function(){} } 

# 3 and # 4 use the constructor function. this means that you can use them to create multiple objects of the same class (classes don't actually exist in JavaScript)

# 4 is different from # 3 because all the objects built by C # 4 will share the method, since it is a property of their prototype. This saves memory (but only a very small amount), and if you change the prototype method, all # 4 objects will be updated immediately - even if they have already been created.

# 1, # 2 and # 5 are all pretty much equivalent. This is due to the fact that maybe someday there will be only one of them at a time, so the fact that No. 2 has a method added to the prototype does not really matter. (excluding cloning)

There are several more ways to add methods to objects using factories with closure or add β€œstatic” properties / methods to functions or private nested functions ... :)

+83
Feb 02 '09 at 21:04
source share

Usually use the prototype property:

 function YourObject() { // } YourObject.prototype.yourMethod= function() { // } 

One thing that I haven't mentioned yet is why you might want to use the prototype property on top of, say, object literal notation: this ensures that the function definition will be used in all instances of objects created from your function prototype , not once per instance.

+13
Feb 02 '09 at 20:44
source share

Don’t worry brother, here is the code:

  var myObj=function(){ var value=null this.setValue=function(strValue){ this.value=strValue; }; this.getValue=function(){ return this.value; }; }; 

You can call this object as follows:

  var obj= new myObj(); obj.setValue("Hi!"); alert(obj.getValue()); 
+2
Oct. 15 '13 at 13:23
source share
 Function.prototype.implement = function(member, value) { this[member] = value; return this; } function MyFunction() { //... } (function($){ $.implement("a", "blabla") .implement("b", function(){ /* some function */ }) .implement("c" {a:'', b:''}); })(MyFunction); 
+1
Feb 08 2018-11-11T00:
source share

With es6 you can do it like this:

 var a = { func(){ return 'The value'; } } document.getElementById('out').innerHTML = a.func(); 
 <div id="out"></div> 
+1
Dec 01 '15 at 12:30
source share

You can also do the following instead of using the Object.create () method.

Function call:

 com.blah.MyChildObj.prototype = createObject(com.blah.MyParentObj.prototype, com.blah.MyChildObj); 

Function Definition:

 function createObject(theProto, theConst) { function x() {}; x.prototype = theProto; x.prototype.constructor = theConst; return new x(); } 
0
May 26 '15 at
source share



All Articles