久久久久久久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(如何讓我的機器人提及發出該機器人命令的人)
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 服務器時的歡迎消息)
主站蜘蛛池模板: 99视频免费看| 在线日韩精品视频 | 日本一区二区三区精品视频 | 国产清纯白嫩初高生视频在线观看 | 亚洲免费一 | 久久机热| 成人午夜视频在线观看 | 日韩在线91 | 日本天天操 | www.日韩高清 | 日韩视频在线一区 | 国产成人精品高清久久 | 亚洲国产一区视频 | 精品一区二区在线观看 | 日韩精品a在线观看图片 | 玖玖免费| 国产精品99免费视频 | 电影91久久久 | 国产一区二区在线免费播放 | 性做久久久久久免费观看欧美 | 午夜私人影院 | 国产精品一区二区在线播放 | 成人在线视频观看 | 亚洲视频在线观看免费 | 成人午夜影院 | 欧美成人专区 | 日韩欧美三区 | 久久久网 | 中文字幕免费观看 | 欧美一区二区在线观看视频 | 日韩精品在线观看一区二区三区 | 国内精品久久久久久久 | 欧美激情黄色 | 日本不卡在线观看 | 免费午夜视频 | 91视频18| 尤物在线视频 | 九九伦理电影 | 欧美一二三 | 国产99免费| 国产羞羞视频在线观看 |