0

I could create an object with some methods, and later add a property to it as follows:

var myObj = (function () {
    var my = {};
    my.method1=function(){}
    my.method2=function(){}
    my.method3=function(){}
    return my;
    }());
myObj.myProperty=123;

How could I create the object first and add a property, and then later add the methods afterwards?

myObj={};
myObj.myProperty=123;
//How do I add the above methods to myObj?
6
  • myObj.my = { method1: function() } Commented Feb 25, 2015 at 21:21
  • Why can't you use myObj.method1 = function() {}? Commented Feb 25, 2015 at 21:21
  • I assume you are not simply doing myObj.method1=function(){}; because you want the functions to be defined in their own scope? Otherwise I don't really see what the issue is. Commented Feb 25, 2015 at 21:21
  • @FelixKling Yes, each of them have their own script. Commented Feb 25, 2015 at 21:22
  • @ShawnBush I suppose I could, but the way I showed just seemed like a clean way to organize them. Commented Feb 25, 2015 at 21:22

3 Answers 3

1

I guess there are two solutions:

Merge the objects:

var myObj = {...};
// ...
var objWithMethods = (function() { ... }());
Object.assign(myObj, objWithMethods);

(Object.assign is an ES6 methods. A polyfill can be found in the link, libraries often also provide a method with similar behavior).

Pass the object the methods should be assigned to as argument:

var myObj = {};
myObj = (function (obj) {
    var my = obj || {};
    my.method1=function(){}
    my.method2=function(){}
    my.method3=function(){}
    return my;
}(myObj));
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Felix. Would you recommend one approach over the other? I assume I will go the with the second option unless you think differently.
At this point, the most important factor is that whoever reads the code understands what's going on.
Well, I definitely understand the second approach, but currently not the first approach. Suppose I should learn both of them. Thanks!
0

You can do an extend operation using an existing object

var myObj = {...}

var myAdditionalMethods = { someMethod : function(){ } }

//extend the object
for(var i in myAdditionalMethods)
   if(!myObj.hasOwnProperty(i)) 
       myObj[i] = myAdditionalMethods[i];

there are a lot of libraries that have this functionality built in, but that is how you would do it without one

Comments

0

Even prototype can add the functions to original object.

 var myObj = function() {
   this.myProperty = 123;
 }

 myObj.prototype.method1 = function method1() {
   alert("method1")
 }
 myObj.prototype.method2 = function method2() {
   alert("method2")
 }
 var newObj = new myObj();
 newObj.method1();
 newObj.method2();
 console.log(newObj)

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.