简体   繁体   中英

Javascript object constructor and setting properties

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

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM