2017-07-13 166 views
0

我有兩個變量(技術上 「常量」 在這裏,他們在這個順序定義):構造函數 「是不是一個構造函數」

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))



提前非常感謝您的回答。

+0

您添加'createVariable1'到'variable2'而而不是'variable2.prototype' - 這就是爲什麼'this'沒有'createVariable1' –

+0

@JaromandaX:這是問答,而不是聊天室;如果您知道答案,請在答案部分以答案形式寫出答案。評論是要求澄清和抨擊人。謝謝! –

+0

@LightnessRacesinOrbit - 我明白這個地方是如何工作的,我沒有添加作爲答案,因爲我不認爲這是代碼中的唯一問題 - 順便說一句:lol @ berating人:p - 我只是檢查一些其他可能出現的代碼問題,如果這對您有用:p –

回答

0

所以,一些調試和上一個JSfiddle位測試後,要弄清楚我是否應該屬性附加到variable2.prototypevariable2我偶然發現了這一點,砸我的頭靠在我的辦公桌上沒有早點注意到它:

在我的功能:

const variable2 = function(someParam){ 
    return new this.createVariable1(someParam); 
} 

this關鍵字(呼叫variable2(/*params*/)時)被綁定到window(因爲這是默認行爲,爲console.log一個電話是真的致電window.console.log)。

因此,我剛剛更換this通過arguments.callee像這樣:

const variable2 = functon(someParam){ 
    return new arguments.callee.createVariable1(someParam); 
} 



這也解決了屬性定義 「問題」。爲了與arguments.callee工作,定義如下:

Object.defineProperty(variable2, "createVariable1", {

如果定義了它的原型,arguments.callee.createVariable1是不確定的。



非常非常感謝那些誰試圖幫助我走出這個:d!


編輯
爲了符合棄用,這裏是一個更新的版本(如果它甚至遠程必要):

const variable2 = function f(someParam){ 
    return new f.createVariable1(someParam); 
} 

而且

const variable1 = function obj(someParam){//constructor function 
    if(obj.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); 
    } 
} 
//I'll be fine with the long ass get the prototype thing, 
//it's only used in this specific part of the code (which is about 1/200th of it) 
-1

,我看到的是問題this裏面

return new this.createVariable1(someParam); 

不是指variable2對象,而是指窗口,因爲variable2不是窗口以外的任何父對象的一部分。如果你在console.log(this)裏面的功能會告訴你這個。 如果你想使發電機的功能,你可以更改您的代碼如下:如下

​​

和發電機:

var creator = new variable2("*"); 
console.log(creator.create()); 
+0

已經解決的問題,再試一次 – Vivick

+0

+解決方案沒有「抽象」對象創建的所有好處(鏈接沒有額外的括號/沒有混淆鏈接/獲取對象沒有明確的'新') – Vivick