javascript 中没有实际的类。但是你必须用你得到的东西工作。
让我们以“类”为例:
var example = function (string) {
this._self = string;
}
有了上述内容,您可以执行以下操作:
var ex = new example("Hello People."),
display = ex._self; // returns "Hello People."
我认为通过使用类似的东西example.prototype.newFun = function(){}
会为该“类”添加一个新属性。但它在我的代码中不起作用。
这是我正在测试的完整代码:
var example = function (string) {
this._self = string;//public, var like, storage
}
var showExample = new example("Hello People");
showExample.prototype.display = function (a) {//code stops here, with error "Uncaught TypeError: Cannot set property 'display' of undefined"
return a;
}
console.log(showExample._self);
console.log(showExample.display("Bye"));
我想要做的是将display
函数作为“公共函数”添加到示例函数中。我可能做错了什么。