I have the following simple example.
(function() {
var note = {
show: function() {
alert('hi');
}
};
})();
When usage
note.show();
Show me error message ReferenceError: note is not defined
. but when using note object without enclose by anonymous function work fine.
Now, how can I use note object outside anonymous function or in other page?
I believe you meant to use something like the module pattern. A very basic example would be:
var note = (function() {
return {
show: function() {
alert('hi');
}
};
}());
This is only useful if you have closures inside, like:
var note = (function() {
var someNumber = 10;
return {
show: function() {
alert('hi');
},
someNumberTimes(n) {
return someNumber * n;
}
};
}());
console.log(note.someNumberTimes(5)); // 50
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.