Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
121
rated 0 times [  125] [ 4]  / answers: 1 / hits: 17691  / 12 Years ago, sat, april 14, 2012, 12:00:00

I have the following code and am wondering how to make the last line work. I addopted a set of api's that current use _view appended as it's namespacing convention and would rather use something like arc.view.$function_name. thx



var arc={};
arc.view={
say_hello: function(){
alert(I want to say hello);
}
}
function say_goodbye(){
alert(goodbye to you);
}

arc.view.say_hello(); // works
window['say_goodbye'](); // works
// possible to make this work?
window['arc.view.say_hello']();

More From » jquery

 Answers
46
window['arc']['view']['say_hello']();


or



window.arc.view.say_hello()


or



window['arc'].view['say_hello']()


Either the dot syntax or the bracket syntax will work. Dot syntax is really just syntactic sugar for a bracket-based property lookup, so all of the above code snippets are identical. Use bracket syntax when the property name itself is a dynamic value, or when using the property name in dot syntax would cause a syntax error. E.g.:



var dynamicMethodName = someObject.getMethodName();
someOtherObject[dynamicMethodName]();


or



someOtherObject[a key string with spaces and {special characters}]();

[#86239] Friday, April 13, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
georgeh

Total Points: 193
Total Questions: 103
Total Answers: 111

Location: United States Minor Outlying Island
Member since Sat, May 28, 2022
2 Years ago
georgeh questions
Fri, Oct 8, 21, 00:00, 3 Years ago
Tue, Jan 5, 21, 00:00, 3 Years ago
Wed, Nov 25, 20, 00:00, 4 Years ago
Sat, Sep 12, 20, 00:00, 4 Years ago
Sat, Mar 7, 20, 00:00, 4 Years ago
;