[js]關(guān)于Object.create(null)的疑惑

問題源碼

// import underscore.js
var o = Object.create(null);//我的理解o應(yīng)該就是空對(duì)象龙亲,創(chuàng)建一個(gè)沒有原型的對(duì)象秧了。對(duì)于for-in屬性遍歷更加有利倦蚪〕蘩耄可以不用遞歸去找原型鏈上的屬性了蹄葱。
console.log(_.isEqual(o,{}));//true
console.log({}.__proto__);//Object
console.log(o.__proto__);//undefined

既然二者對(duì)象相等峦筒,為什么{}沒有proto屬性呢究西?

解惑

Object.create(null) 困惑

這里面d=Object.create(null);結(jié)果沒有任何屬性,是一個(gè)徹徹底底的空對(duì)象物喷。
這里面c={},可以看出來瀏覽器給它實(shí)現(xiàn)了__proto__屬性
而實(shí)際上__proto__屬性是不建議使用的卤材,是瀏覽器自身實(shí)現(xiàn)的結(jié)果。

The use of __proto__ is controversial, and has been discouraged. It was never originally included in the EcmaScript language spec, but modern browsers decided to implement it anyway. Only recently, the __proto__ property has been standardized in the ECMAScript 6 language specification for web browsers to ensure compatibility, so will be supported into the future.

o = {};// 以字面量方式創(chuàng)建的空對(duì)象就相當(dāng)于:o = Object.create(Object.prototype);

遺留問題

isEqual是如何判斷對(duì)象相等的峦失?

_.isEqual = function(a, b) {
    return eq(a, b, [], []);
  };
// Internal recursive comparison function for `isEqual`.
  var eq = function(a, b, aStack, bStack) {
    // Identical objects are equal. `0 === -0`, but they aren't identical.
    // See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal).
    if (a === b) return a !== 0 || 1 / a == 1 / b;
    // A strict comparison is necessary because `null == undefined`.
    if (a == null || b == null) return a === b;
    // Unwrap any wrapped objects.
    if (a instanceof _) a = a._wrapped;
    if (b instanceof _) b = b._wrapped;
    // Compare `[[Class]]` names.
    var className = toString.call(a);
    if (className != toString.call(b)) return false;
    switch (className) {
      // Strings, numbers, dates, and booleans are compared by value.
      case '[object String]':
        // Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
        // equivalent to `new String("5")`.
        return a == String(b);
      case '[object Number]':
        // `NaN`s are equivalent, but non-reflexive. An `egal` comparison is performed for
        // other numeric values.
        return a != +a ? b != +b : (a == 0 ? 1 / a == 1 / b : a == +b);
      case '[object Date]':
      case '[object Boolean]':
        // Coerce dates and booleans to numeric primitive values. Dates are compared by their
        // millisecond representations. Note that invalid dates with millisecond representations
        // of `NaN` are not equivalent.
        return +a == +b;
      // RegExps are compared by their source patterns and flags.
      case '[object RegExp]':
        return a.source == b.source &&
               a.global == b.global &&
               a.multiline == b.multiline &&
               a.ignoreCase == b.ignoreCase;
    }
    if (typeof a != 'object' || typeof b != 'object') return false;
    // Assume equality for cyclic structures. The algorithm for detecting cyclic
    // structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
    var length = aStack.length;
    while (length--) {
      // Linear search. Performance is inversely proportional to the number of
      // unique nested structures.
      if (aStack[length] == a) return bStack[length] == b;
    }
    // Objects with different constructors are not equivalent, but `Object`s
    // from different frames are.
    var aCtor = a.constructor, bCtor = b.constructor;
    if (aCtor !== bCtor && !(_.isFunction(aCtor) && (aCtor instanceof aCtor) &&
                             _.isFunction(bCtor) && (bCtor instanceof bCtor))
                        && ('constructor' in a && 'constructor' in b)) {
      return false;
    }
    // Add the first object to the stack of traversed objects.
    aStack.push(a);
    bStack.push(b);
    var size = 0, result = true;
    // Recursively compare objects and arrays.
    if (className == '[object Array]') {
      // Compare array lengths to determine if a deep comparison is necessary.
      size = a.length;
      result = size == b.length;
      if (result) {
        // Deep compare the contents, ignoring non-numeric properties.
        while (size--) {
          if (!(result = eq(a[size], b[size], aStack, bStack))) break;
        }
      }
    } else {
      // Deep compare objects.
      for (var key in a) {
        if (_.has(a, key)) {
          // Count the expected number of properties.
          size++;
          // Deep compare each member.
          if (!(result = _.has(b, key) && eq(a[key], b[key], aStack, bStack))) break;
        }
      }
      // Ensure that both objects contain the same number of properties.
      if (result) {
        for (key in b) {
          if (_.has(b, key) && !(size--)) break;
        }
        result = !size;
      }
    }
    // Remove the first object from the stack of traversed objects.
    aStack.pop();
    bStack.pop();
    return result;
  };

延伸問題

  1. 0和-0有什么區(qū)別扇丛?
    回答:0 和 -0 內(nèi)存表示不同。64位的double尉辑,第64位用于表示數(shù)的符號(hào)帆精,0的那位是1,-0的那位是0隧魄。
    可以參考:
    http://www.cnblogs.com/ziyunfei/archive/2012/12/10/2777099.html
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末卓练,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子购啄,更是在濱河造成了極大的恐慌襟企,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,430評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件狮含,死亡現(xiàn)場離奇詭異顽悼,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)辉川,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,406評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門表蝙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人乓旗,你說我怎么就攤上這事府蛇。” “怎么了屿愚?”我有些...
    開封第一講書人閱讀 167,834評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵汇跨,是天一觀的道長。 經(jīng)常有香客問我妆距,道長穷遂,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,543評(píng)論 1 296
  • 正文 為了忘掉前任娱据,我火速辦了婚禮蚪黑,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己忌穿,他們只是感情好抒寂,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,547評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著掠剑,像睡著了一般屈芜。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上朴译,一...
    開封第一講書人閱讀 52,196評(píng)論 1 308
  • 那天井佑,我揣著相機(jī)與錄音,去河邊找鬼眠寿。 笑死躬翁,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的盯拱。 我是一名探鬼主播姆另,決...
    沈念sama閱讀 40,776評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼坟乾!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起蝶防,我...
    開封第一講書人閱讀 39,671評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤甚侣,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后间学,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體殷费,經(jīng)...
    沈念sama閱讀 46,221評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,303評(píng)論 3 340
  • 正文 我和宋清朗相戀三年低葫,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了详羡。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,444評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡嘿悬,死狀恐怖实柠,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情善涨,我是刑警寧澤窒盐,帶...
    沈念sama閱讀 36,134評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站钢拧,受9級(jí)特大地震影響蟹漓,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜源内,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,810評(píng)論 3 333
  • 文/蒙蒙 一葡粒、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦嗽交、人聲如沸卿嘲。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,285評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽腔寡。三九已至,卻和暖如春掌唾,著一層夾襖步出監(jiān)牢的瞬間放前,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,399評(píng)論 1 272
  • 我被黑心中介騙來泰國打工糯彬, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留凭语,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,837評(píng)論 3 376
  • 正文 我出身青樓撩扒,卻偏偏與公主長得像似扔,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子搓谆,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,455評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • 在JavaScript中炒辉,一切都是對(duì)象。 使用深度優(yōu)先遍歷,element是一個(gè)DOM元素泉手,selectors是一...
    garble閱讀 579評(píng)論 0 1
  • You Don't Know JS: this & Object Prototypes Chapter 5: Pr...
    大橙子CZ閱讀 579評(píng)論 0 2
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,569評(píng)論 0 23
  • 學(xué)習(xí) 每天固定項(xiàng)目 裝修 到了選瓷磚地板環(huán)節(jié)黔寇,定下客廳門庭餐廳都用地板了,某些地方要返工下斩萌。 育兒 倆娃在家比較辛...
    張家小林閱讀 204評(píng)論 0 2
  • 美女行且來缝裤,綾羅綠玉裁。 低頭天色美颊郎,一笑杏花白憋飞。 多情佳公子,萬金酬風(fēng)采姆吭。 不知寒門客榛做,一飯幾擔(dān)柴。
    蒔芬繡氣閱讀 184評(píng)論 0 0