where does the javascript function reside and what is the value -
var test = (function(){ this.message = function(){ alert('hi'); }; })();
i have 2 questions
where above function go? go dom class name test? how create new object every time , phone call function.what want create class info structure, create objects using modular javascript. have tried above code, however, sense message function in window object rather entire class.
let's rewrite code next rough equivalent:
var test; function foobar() { this.message = function() { alert('hi'); } } foobar(); the foobar function represents anonymous function gets invoked.
inside function, this reference window (in browser), adds message function window, achieving following:
window.message = function() { alert('hi'); }; however, if wish create objects need utilize new, e.g.:
function test() { this.message = function() { alert('hi'); }; } var x = new test(); x.message(); // alerts hi as mentioned null in comments, it's recommended utilize title casing when function used new, i.e. function test() { ... }.
javascript
No comments:
Post a Comment