這裏是我會做:
function Parser() {
this.myVar = "";
}
Parser.prototype.initWithString = function (inputString) {
this.myVar = "somethingAfterParse";
};
module.exports = Parser;
你的方法,註釋
module.exports = function Parser() {
// public property "myVar" of any new Parser object
this.myVar = "";
// anonymous function assigned to private variable "parse"
var parse = function (inputString) {
// "this" will likely be the global object here, dangling error
this.myVar = "somethingAfterParse";
}
// return API object - unnecessary and makes debugging harder (*)
return {
// anonymous function that calls private "parse" function
initWithString: function(inputString) {
// calling parse without defining "this", definitely an error (**)
parse(inputString)
},
// the above is equivalent to and therefore better written as
// initWithString: parse,
// copy of (!) public property myVar, definitely an error (***)
name: this.myVar
};
};
當你遺漏了冗餘位(並使用的原型鏈)你最終以我上面的建議。
*我知道這是用於定義對象的公共接口一個流行的方法。我個人不喜歡它。從JS構造函數返回一個對象有效地打破了new
運營商的用處:
**this
在函數內部將指功能叫上(在背景)的任何對象。
通常點符號爲您服務:object.method()
套this
到object
裏面method
。
但是,您不要在任何對象上調用parse()
函數。在沒有上下文的情況下調用的任何函數都會在全局對象的上下文中運行(在Window
的瀏覽器中)。
您將需要通過使用var self = this;
前面的initWithString
正文以及parse.apply(self, arguments)
正文指定上下文。
最後,「讓我們返回一個對象作爲API」的方法使事情變得比他們需要的更困難。
***原始類型總是由JavaScript中的值賦值。就像數字或布爾值一樣,您不能引用字符串。將它們分配給其他變量會複製它們。
你鴕鳥政策需要實例只使用Parser.initWithString()。在你的解析函數解析後返回this.name ... – solick