0
比方說,我定義下面的類在我AngularJs代碼中使用:類在AngularJS - 屬性和方法
app.factory("exampleClass", function() {
// constructor
function exampleClass(val) {
this.prop1 = val;
this.result = "";
}
// methods
exampleClass.prototype = {
doSomething: function (userVal) {
this.result = this.prop1 + userVal; // any way to avoid this?
console.log(this.result);
}
};
// return class
return (exampleClass);
});
// .. later in code
new exampleClass("another").doSomething("321");
console.log(scope.example.prop1);
有什麼辦法來逃避引用類字段(PROP1和結果)與此有關。在doSomething方法中,仍然可以直接從課外引用它們?很明顯,我試圖在其他現代語言中效仿類似public type fieldName
的東西 - 當這個。先不查看實例變量?