下面给出了例子:一个名为"say"的实例方法,一个名为"say"的内部函数,和同样名为"say"的静态方法。这三种不同的方法,都是彼此独立地存在的。
/** @constructor */
Person = function() {
// 实例方法
this.say = function() {
return "I'm an instance.";
}
// 内部方法
function say() {
return "I'm inner.";
}
}
// 静态方法
Person.say = function() {
return "I'm static.";
}
var p = new Person();
p.say(); // I'm an instance. 实例方法
Person.say(); // I'm static. 静态方法
// 这里无法直接访问内部函数 there is no way to directly access the inner function from here