Using properties inside a Javascript object literal -


I am trying to create javascript json objects which have a dynamic field that will return a value based on previously specified properties. . So far it is what I have ...

  var car = {color: 'red', tires: 4, hubbaps: 'spinning', age: 4, full_name: this. Color + '' + this.spinning}  

I was thinking that this is possible? My goal is to be able to reference values ​​such as car.color or car.full_name . For this example, the car.full_name value can be red spinning

Wrap your object in a function so you can take action on it, eg Concatenations

  var car = function () {var color = 'red', hubcaps = 'spinning', full_name = color + '+ hubcaps; Return {Color: color, wheels: 4, hubs: hubbies, age: 4, full_name: full_name}; // (); & Lt; - Anonymous call after defining it}} (); // true car.full_name === "red spinning";  Edit:  As @gimit 1988, if you want to instantiate new versions of these functions, that means oo javascript, you call inline function  ();  and use it 

  var myCar = new car (); Warning (mycar.color);  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -