2016-05-10 28 views
0

我已經構造一個相當有用的功能,以確定數據類型;然而,在編碼愉快的時候,我被一個相當令人擔憂的困境粗暴地打斷了。的JavaScript - 訪問屬性後(閉合).bind()

正如您所知,在關閉後調用.bind({foo:'bar'})後,您無法訪問foo屬性「外部」;然而,在關閉,this.foo工程。

而且,以這樣的方式分配東西的時候,你經常會遇到一拋:intermediary ... blah blah is undefined當您嘗試訪問屬性 - 定義後直接。下面的代碼解決這些問題,但...

問題是代碼後解釋說:

"use strict"; 

if ('undefined' == typeof global) 
{ 
    Object.defineProperty 
    (
     window,'global', 
     { 
      writable:false, 
      configurable:false, 
      enumerable:false, 
      value:window 
     } 
    ); 
} 


Object.defineProperty 
(
    Function.prototype, 'wrap', 
    { 
     writable:false, 
     enumerable:false, 
     configurable:false, 

     value:function(jsob) 
     { 
      this.bind(jsob); 

      for (var i in jsob) 
      { this[i] = jsob[i]; } 

      return this; 
     } 
    } 
); 


global.typeOf = function(data) 
{ 
    if ((data === null) || (data === undefined)) 
    { return 'void'; } 

    if ((data === true) || (data === false)) 
    { return 'bool'; } 

    var tpof = (({}).toString.call(data).match(/\s([a-zA-Z]+)/)[1].toLowerCase()); 

    if ((tpof == 'array') || (tpof == 'htmlcollection') || (tpof == 'namednodemap')) 
    { return 'list'; } 

    if ((tpof == 'global') || (tpof == 'window')) 
    { return 'glob'; } 

    switch (tpof.substr(0,6)) 
    { 
     case 'number': return 'unit'; 
     case 'string': return (/[^\x20-\x7E\t\r\n]/.test(data) ? 'blob' : 'text'); 
     case 'object': return 'jsob'; 
     case 'functi': return 'func'; 

     default: return 'node'; 
    } 
} 
.wrap 
({ 
    list:'void bool unit text blob list jsob func node glob'.split(' '), 
    init:function() 
    { 
     this.list.forEach(function(item) 
     { 
      global[(item.toUpperCase())] = item; 
      global[('is'+(item[0].toUpperCase() + item.substr(1,item.length)))] = function(data) 
      { 
       return ((typeOf(data) == this.text) ? true : false); 
      } 
      .bind({text:item.toLowerCase()}); // <-- ISSUE 
     }); 

     return this; 
    } 
}).init(); 

所以上面的小wrapper照顧這種古怪的;然而,看看<-- ISSUE是哪一行;看,我不能使用wrap()那裏,我不得不使用bind(),否則 - 裏面的功能 - this是未定義的!

讓我澄清一下:如果你使用的整個代碼只是因爲它是上面一個品牌打屁股新的HTML文件中<script>標籤;只需將ISSUE行的bind字改爲:wrap;然後嘗試類似:isText("bite me!");

你會看到指定類似的錯誤:

無法讀取未定義的屬性 「文本」 ..

左右;如果您在該函數定義中執行console.log(this);你會看到undefined

如果有人可以幫助解決這個,或者至少解釋爲什麼發生這種情況,我會很感激輸入。

+0

@Kashif ::'wrap()'方法可以從閉包訪問屬性-external。它縮短了所需的代碼。 – argon

+0

@deceze ::所有的代碼可能是問題的一部分;和 - 代碼是可用的,就像它,只是複製+粘貼& test;什麼都不是「失蹤」。 – argon

+1

似乎您遇到的問題與您正在嘗試修復的問題相同。包裝好的isX函數不能從封裝的閉包中訪問'this'。我懷疑你實際上是在詢問你的「奇怪」的解釋 – Tibrogargan

回答

1

我看絕對沒有目的這個wrap功能。實際上,根本沒有理由在此用例中使用thisbind。只是做

global.typeOf = function(data) { 
    if (data == null) return 'void'; 
    switch (typeof data) 
     case "boolean": return 'bool'; 
     case "number": return 'unit'; 
     case "string": return /[^\x20-\x7E\t\r\n]/.test(data) ? 'blob' : 'text'; 
    } 
    switch (Object.prototype.toString.call(data).slice(8, -1).toLowerCase()) { 
     case "array": 
     case "htmlcollection": 
     case "namednodemap": return 'list'; 
     case "global": 
     case "window": return 'glob'; 
     case "object": return 'jsob'; 
     case "function": return 'func'; 
     default: return 'node'; 
    } 
}; 
global.typeOf.list = 'void bool unit text blob list jsob func node glob'.split(' '); 

global.typeOf.list.forEach(function(item) { 
    global[item.toUpperCase()] = item; 
    global['is'+item[0].toUpperCase()+item.slice(1)] = function(data) { 
     return typeOf(data) == item; 
    } 
}); 
+0

「綁定」(包裝)的要點是主類型列表應該包含在它所屬的地方,因爲我還有另一個函數:「kindOf」 - 它檢查輔助數據類型,它需要一個引用相應地命名和定義常量和函數。它可以通過將它分配給後來完成,但我正在尋找一種方法來分配一次,而不必在定義它時自己的範圍之外重新引用「typeOf」。這是因爲這些全局變量在定義後應該是不可變的;代碼是更長的哈哈,這是簡短的版本:) – argon

+0

如果你可以合併該列表與typeOf閉包,以這樣的方式,它可以被訪問,如:'console.log(typeOf.list)',如果你的代碼也可以在NodeJS中工作,那麼我會很樂意接受它。當前「isFunc」賦值的問題是,比較檢查變量「item」,該變量在調用「isFunc」時可能不在範圍內,因此,這也是「綁定」因爲它包含自己的價值。 – argon

+0

當您使用它時,在「詞法作用域」上發現了一些有趣的帖子:https://developer.mozilla.org/en/docs/Web/JavaScript/Closures and .. here:https://spin.atomicobject.com/2014/10/20/javascript-scope-closures/....它可能會導致問題,如果不謹慎使用,但...看看這裏:https://www.toptal.com/javascript/10-most-common- javascript-errors – argon