How can I declare non-global static methods in js?
foo.bar = function() {
function testing{
console.log("statckoverlow rocks!");
}
function testing2{
console.log("testing2 funtction");
}
}
How can I call the testing functions? I am a newbie in JS.
Thanks for help.
You probably want an object.
foo.bar = {
testing: function() {
console.log("statckoverlow rocks!");
},
testing2: function() {
console.log("testing2 funtction");
}
};
Then, call foo.bar.testing()
, for example.
You could do like this:
foo.bar = (function() {
var testing = function () {
console.log("statckoverlow rocks!");
};
var testing2 = function () {
console.log("testing2 funtction");
};
return {
testing: testing,
testing2: testing2
};
}());
// call them
foo.bar.testing();
foo.bar.testing2();
Did you mean:
var foo = {
bar: {
testing: function()
{
console.log("statckoverlow rocks!");
},
testing2: function()
{
console.log("testing2 funtction");
}
}
};
foo.bar.testing();
foo.bar.testing2();
// Constructor
function Foo() {
var myvar = 'hey'; // private
this.property = myvar;
this.method = function() { ... };
}
Foo.prototype = {
staticMethod: function() {
console.log( this.property );
}
}
var foo = new Foo();
foo.staticMethod(); //=> hey
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.