/    Sign up×
Community /Pin to ProfileBookmark

Hello. I am trying to create an object. Now when I try to assign a value to a property using another property of the object, javascript creates errors.

[code=php]var math={

four:2*2,
six:(polo.four)*3,

}

alert(math.six)[/code]

I am getting math is undefined error. It is certainly about [B]polo.four[/B] . Why cant I reference polo inside polo object? How can I reference another object property to get its value in an object?

Thanks

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledOct 18.2008 — remove the comma (,) in the last line of the object declaration
<i>
</i>var math={

four:2*2,
six:(polo.four)*3[b],[/b]
}


tried the code even with the comma, worked for me as far [b]polo.four[/b] is defined. if i can remind, msie don't like comma in the last line of object statement.
Copy linkTweet thisAlerts:
@kineemauthorOct 18.2008 — So sorry. Forget to change polo to math. Actual code is this:
[code=php]
var math={

four:2*2,
six:(math.four)*3,

}

alert(math.six)[/code]
Copy linkTweet thisAlerts:
@ZeroKilledOct 18.2008 — no, you can't do that becuase the syntax you are using to create the object. i'm not sure, but the flow execution of javascript on this kind of syntax is that it first create something like an anonymous object before assigning it to the variable. so, when it read the line [b][noparse]six?math.four)*3,[/noparse][/b], math object still not exist. however, if you replace [b]math.four[/b] with [b]this.four[/b] you will get NaN.

--- edit ---

some mistake is said in this post about the [b]this[/b] keyword. correction are comented on my next post to reduce confusion.
Copy linkTweet thisAlerts:
@kineemauthorOct 18.2008 — no, you can't do that becuase the syntax you are using to create the object. i'm not sure, but the flow execution of javascript on this kind of syntax is that it first create something like an anonymous object before assigning it to the variable. so, when it read the line [b][noparse]six?math.four)*3,[/noparse][/b], math object still not exist. however, if you replace [b]math.four[/b] with [b]this.four[/b] you will get NaN.[/QUOTE]

Thanks for the reply. But how will I reference the [B]math.four[/B] property. To create [B]math.six[/B] propety I need [B]math.four[/B] property value.
Copy linkTweet thisAlerts:
@ZeroKilledOct 18.2008 — sorry, did a mistake on my previous post. using the [b]this[/b] keyword in that syntax, [b]this[/b] refer to the scope in which is executed. in your sample, [b]this[/b] refer to the window object, i thought it will refer the object, but that's not true. however, you simply can't refer to any of it's property in that syntax until the object is stored in a variable. that is the backward using that syntax.
Copy linkTweet thisAlerts:
@kineemauthorOct 18.2008 — Is using a method like init efficient or are there any other solution better?

[code=php]var math={

four:2*2,

six:null,

init:function(){

math.six=(math.four)*3;

},

}
math.init();
alert(math.six)[/code]
Copy linkTweet thisAlerts:
@kineemauthorOct 21.2008 — Any comment? Is using an init efficient or not?
Copy linkTweet thisAlerts:
@toicontienOct 21.2008 — This might be a tad more efficient and easier to maintain:
function math(multiplier) {
this.six = this.four * multiplier;
}
math.prototype = {

four : 2*2,

six : null

};

var m1 = new math(3);
alert(m1.six);
×

Success!

Help @kineem spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...