久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Javascript“流行"從對象

Javascript quot;popquot; from object(Javascript“流行從對象)
本文介紹了Javascript“流行"從對象的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我編寫了以下代碼來從對象中彈出"一個屬性,就好像它是一個數組一樣.這看起來像是會讓我被更嚴肅的程序員打的那種代碼,所以我想知道這樣做的正確方法是什么:

I wrote the following code to "pop" a property from an object as if it were an array. This looks like the kind of code that would get me slapped by more serious programmers, so I was wondering what is the proper way to do this:

// wrong way to pop:
for( key in profiles ){
    var profile = profiles[key];  // get first property
    profiles[key] = 0;            // Save over property just in case "delete" actually deletes the property contents instead of just removing it from the object
    delete profiles[key];         // remove the property from the object
    break;                        // "break" because this is a loop
}

我應該在上面提到,與真正的流行音樂"不同,我不需要對象以任何特定的順序出現.我只需要取出一個并將其從其父對象中刪除即可.

I should have mentioned above, that unlike a true "pop", I don't need the objects to come out in any particular order. I just need to get one out and remove it from its parent object.

推薦答案

for( key in profiles ){

你真的應該將 key 聲明為 var.

You should really declare key as a var.

profiles[key] = 0;            // Save over property just in case "delete" actually deletes the property contents instead of just removing it from the object

是不必要的.刪除不會觸及屬性的值(或者對于有setter但沒有getter的屬性,甚至要求它有一個值).

is unnecessary. Delete doesn't touch the value of the property (or in the case of a property that has a setter but no getter, even require that it have a value).

如果對象在其原型上有任何可枚舉的屬性,那么這會做一些奇怪的事情.考慮

If the object has any enumerable properties on its prototype, then this will do something odd. Consider

Object.prototype.foo = 42;

function take(obj) {
  for (var key in obj) {
    // Uncomment below to fix prototype problem.
    // if (!Object.hasOwnProperty.call(obj, key)) continue;
    var result = obj[key];
    // If the property can't be deleted fail with an error.
    if (!delete obj[key]) { throw new Error(); }
    return result;
  } 
}

var o = {};
alert(take(o));  // alerts 42
alert(take(o));  // still alerts 42

這篇關于Javascript“流行"從對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發(fā)出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 一区二区三区国产精品 | 国产性生活 | 久热在线| 国产午夜精品一区二区三区四区 | 一级免费毛片 | 中国免费毛片 | 免费在线成人网 | 成人三级视频 | 国产理论在线 | 欧美在线观看视频 | 国产一级在线观看 | 黄色三级视频网站 | 黄色大片在线免费观看 | 欧美大片91 | 日本少妇做爰全过程毛片 | 亚洲综合激情五月久久 | 五月婷婷丁香花 | 国产一区二区三区在线观看视频 | 亚洲福利片 | 中文字幕av网站 | 九九九免费视频 | 三级黄色网 | 九九热在线视频观看 | 欧美一级网站 | 伊人久久国产 | 九九视频在线免费观看 | 天堂成人在线 | 国产精品欧美日韩 | av网在线观看 | 香蕉视频一区 | av一区二区三区在线观看 | 黄a视频 | www.99色| 99久久综合 | 男女激情视频网站 | 狠狠干影院 | 亚洲欧美视频一区 | 97精品久久| 欧美日韩精品一区二区三区 | 欧美高清在线 | 亚洲精选一区 |