0

In a scenario such as this when we have an object,

Object1 = function(param1){

        this.attribute1=function(param1){
            console.log("executing");
            //random business logic could be anything 
            var newRelationship;

            switch (param1){
                case "epic": newRelationship= new Epic([this.startPoint, this.endPoint]);
                break;
                case "lame": newRelationship = "lamest";
                break;
            }
            console.log(newRelationship);
             return newRelationship;
        }
}

The object property doesn't actually get set upon a constructor call such as var moveCircle = new Object1("epic), meaning that if any other attributes are dependent on this one object attribute we will have some issues.

One solution is to implement a setter and call it right after object construction, to set our attribute, however this means there would be no need to have the parameter in the object constructor signature.

Object1 = function(){

        this.attribute1=""
        this.setAttribute1 = function(param1){
            console.log("executing");
            //random business logic could be anything 
            var newRelationship;

            switch (param1){
                case "epic": newRelationship= new Epic([this.startPoint, this.endPoint]);
                break;
                case "lame": newRelationship = "lamest";
                break;
            }
            console.log(newRelationship);
             this.attribute1 = newRelationship;
        }
}

however for some reason(can't think of one specifically) we just want to or need to have the parameter as part of the constructor, what is the best approach to make sure it gets set upon creating a new instance of an object type? The solution I have come up with is to simply have the attribute anonymous function be self declaring, however in this scenario, anytime the attribute is accessed during run times, the "business logic" is being re run which is silly.

 Object1 = function(param1){

        this.attribute1=function(param1){
            console.log("executing");
            //random business logic could be anything 
            var newRelationship;

            switch (param1){
                case "epic": newRelationship= new Epic([this.startPoint, this.endPoint]);
                break;
                case "lame": newRelationship = "lamest";
                break;
            }
            console.log(newRelationship);
             return newRelationship;
        }
}()

Can some one tell me what the best approach to solve this problem is, what is a common practice and what realistic scenario would there be in which using a setter and omitting the parameter in the object signature would not be pheasable

0

1 Answer 1

1

First declare function, then use it in constructor.

Object1 = function(param1){

  function foo(param1){
    console.log("I am called only once!");
    //random business logic could be anything 
    var newRelationship;

    switch (param1){
      case "epic": newRelationship = "epic";
        break;
      case "lame": newRelationship = "lamest";
        break;
    }
    console.log(newRelationship);
    return newRelationship;
  }
  this.attribute1 = foo(param1);
}

var obj = new Object1('epic');
obj.attribute1;
obj.attribute1;
obj.attribute1;
obj.attribute1;
obj.attribute1;
obj.attribute1;
console.log(obj.attribute1);

Sign up to request clarification or add additional context in comments.

2 Comments

However with your approach anytime we acess attribute 1 all the code in the function foo will be executed again wouldnt it? Isn't that really inefficient especially if attribute 1 is the result of some very cpu heavy computations based on initialization variables? I had already recognized your suggestion as a possible solution but wanted to know a better way of doing it.
no, it's not called again. It's called only on construction. Notice that attribute1 is a return value of a foo(param1) call, not a function itself.

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.