我有兩個變量(技術上 「常量」 在這裏,他們在這個順序定義):構造函數 「是不是一個構造函數」
const variable1 = function(someParam){//constructor function
if(arguments.callee.caller === this.constructor.prototype.variable2ID){
if(/*some stuff*/){
/*some setup using the this keyword*/
}else
return this.constructor.prototype.variable2ID(document).ready(someParam);
}else{
return Object.create(null);
}
}
和
const variable2 = function(someParam){
return new this.createVariable1(someParam);
}
變量1有一些設置用於繼承及其構造函數:
variable1.prototype = Object.create(Array.prototype);
variable1.prototype.constructor = variable1;
後來,我(再次,按照這個順序)定義一些屬性:
Object.defineProperty(variable1.prototype, "variable2ID", {
value: variable2,
__proto__: variable2.prototype,
enumerable: false,
configurable: false,
writable: false
});
和
Object.defineProperty(variable2, "createVariable1", {
value: variable1,
__proto__: variable1.prototype,
enumerable: false,
configurable: false,
writable: false
});
我的問題是這樣的:
當我打電話給variable2("*")
時,會報錯: TypeError: this.createVariable1 is not a constructor
。
我不明白爲什麼會拋出這個錯誤,因爲存儲在variable2.createVariable1
中的函數是variable1
這是一個構造函數。
當我明確地呼叫new variable2.createVariable1("*")
時,它確實打算做什麼(即撥打variable1
並返回Object.create(null)
,因爲沒有從variable2
中調用)。
我的問題是這樣的:
你能幫我找出我做錯了,而試圖創建與this
關鍵字功能中的對象? (如何實現內variable2
調用構造函數(variable1
))
提前非常感謝您的回答。
您添加'createVariable1'到'variable2'而而不是'variable2.prototype' - 這就是爲什麼'this'沒有'createVariable1' –
@JaromandaX:這是問答,而不是聊天室;如果您知道答案,請在答案部分以答案形式寫出答案。評論是要求澄清和抨擊人。謝謝! –
@LightnessRacesinOrbit - 我明白這個地方是如何工作的,我沒有添加作爲答案,因爲我不認爲這是代碼中的唯一問題 - 順便說一句:lol @ berating人:p - 我只是檢查一些其他可能出現的代碼問題,如果這對您有用:p –