假设我有这个类:
function classA(n){
this.name = n
}
classA.prototype.getName = function(){
return this.name
}
var x = new classA('john')
console.log(x.getName())
我的问题是:我可以将多个方法分组到一个命名空间中吗?所以我想这样做:
var x = new classA('john')
console.log(x.CONSTANT.getName())
所以我想将一些方法称为 x.someMethod() 并将其他方法称为 x.CONSTANT.otherMethod()
PS:我正在寻找一种跨浏览器的方法。绑定(bind)在 Safari 和 IE9 中不起作用。
最佳答案
例如,您可以通过 bind
来实现。 Google es5 shim
用于在浏览器中实现绑定(bind),这些浏览器本身不支持它。
function MyClass(name) {
this.name = name;
this.CONSTANT.otherMethod = this.CONSTANT.otherMethod.bind(this);
}
MyClass.prototype.CONSTANT = {
otherMethod: function() {
alert(this.name);
}
};
关于javascript - 在 JavaScript 的原型(prototype)名称中使用 "dot",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9513557/