简体   繁体   English

不使用Delete运算符从对象中删除属性

[英]Remove a property from the object without using the Delete operator

I am learning about new keywords and have homework 1. I wrote a code to remove a property from the object using the delete operator. 我正在学习new关键字和做作业1.我编写了一个代码,使用delete运算符从对象中删除属性。 So is there any way I don't use delete operator, I still remove it? 那么有什么方法我不使用删除操作符,我仍然删除它? And using new keyword? 并使用new关键字?

var student = { 
  name : "David Rayy", 
  sclass : "VI", 
  rollno : 12 
};

function updateObj() {
  var result = delete student.rollno;
  return student;
}

updateObj()

/* expect
    var student = { 
      name : "David Rayy", 
      sclass : "VI", 
    }; 
*/
  1. Write a function to show all info of student. 写一个功能来显示学生的所有信息。 I haven't done it yet 我还没有这样做
var student = {
  name: 'Herry',
  gender: 'male',
  age: 18
}

function showInfo(obj) {

}

showInfo();

This question is a duplicate of "How do I remove a property from a JavaScript object?", because that question does discuss the alternative. 这个问题是“如何从JavaScript对象中删除属性?”的副本,因为该问题确实讨论了替代方案。

The "alternative", in this case, is student.rollno = undefined . 在这种情况下,“替代”是student.rollno = undefined

This method also has the advantage of being 100 times faster than delete . 该方法还具有比delete快100倍的优点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM