Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript分配和删除属性_Javascript_Object_Properties_Assign - Fatal编程技术网

Javascript分配和删除属性

Javascript分配和删除属性,javascript,object,properties,assign,Javascript,Object,Properties,Assign,有人能解释一下这里到底发生了什么吗 变量myObject={ 价格:20.99, 获取价格:函数{ 退回这个价格; } }; var customObject=Object.createmyObject; customObject.price=19.99; 删除customObject.price; console.logcustomObject.get_price//返回20.99这是由于以下原因造成的。声明 var customObject = Object.create(myObject

有人能解释一下这里到底发生了什么吗

变量myObject={ 价格:20.99, 获取价格:函数{ 退回这个价格; } }; var customObject=Object.createmyObject; customObject.price=19.99; 删除customObject.price;
console.logcustomObject.get_price//返回20.99这是由于以下原因造成的。声明

var customObject = Object.create(myObject);
创建其原型集为myObject的对象。现在您正在分配和删除其上的房产价格。但它并没有改变原型中已经存在的东西

var myObject = {
  price: 20.99,
  get_price: function() {
    return this.price;
  }
};
var customObject = Object.create(myObject);
customObject.price = 19.99; // *
delete customObject.price; // **
console.log(customObject.get_price());
您可以通过在删除房产价格之前将customObject打印到控制台来尝试。您将看到该对象包含一个属性价格,其值设置为19.99,但其_uproto__;属性的价格仍然为20.99。运行以下代码段,并在浏览器控制台中观察输出

变量myObject={ 价格:20.99, 获取价格:函数{ 退回这个价格; } }; var customObject=Object.createmyObject; customObject.price=19.99;
console.logcustomObject 这种情况的发生是由于战争的后果。声明

var customObject = Object.create(myObject);
创建其原型集为myObject的对象。现在您正在分配和删除其上的房产价格。但它并没有改变原型中已经存在的东西

var myObject = {
  price: 20.99,
  get_price: function() {
    return this.price;
  }
};
var customObject = Object.create(myObject);
customObject.price = 19.99; // *
delete customObject.price; // **
console.log(customObject.get_price());
您可以通过在删除房产价格之前将customObject打印到控制台来尝试。您将看到该对象包含一个属性价格,其值设置为19.99,但其_uproto__;属性的价格仍然为20.99。运行以下代码段,并在浏览器控制台中观察输出

变量myObject={ 价格:20.99, 获取价格:函数{ 退回这个价格; } }; var customObject=Object.createmyObject; customObject.price=19.99;
console.logcustomObject Javascript中的所有对象都有一个_proto__属性,称为其原型。它与某些函数的prototype属性不同,原因我将不讨论

原型链是在请求时查找某个名称的顺序。例如,dog.bark将在dog中搜索“bark”,但如果dog没有“bark”属性,则它将在dog中搜索“bark”。uu proto_u等等,直到它到达原型链的末端

使用括号语法{}声明的新对象将对象作为其原型

var myObject = {
  price: 20.99,
  get_price: function() {
    return this.price;
  }
};
var customObject = Object.create(myObject);
customObject.price = 19.99; // *
delete customObject.price; // **
console.log(customObject.get_price());
函数Object.createsome返回一个新对象,该对象的原型为某个对象。有些可以为null以创建没有原型的对象

var myObject = {
  price: 20.99,
  get_price: function() {
    return this.price;
  }
};
var customObject = Object.create(myObject);
customObject.price = 19.99; // *
delete customObject.price; // **
console.log(customObject.get_price());
在*中,对象customObject如下所示:

customObject = {
  price: 19.99,
  __proto__: {
    price: 29.99,
    get_price: function() { return this.price; }
  }
};
customObject = {
  __proto__: {
    price: 29.99,
    get_price: function() { return this.price; }
  }
};
在**中,对象customObject如下所示:

customObject = {
  price: 19.99,
  __proto__: {
    price: 29.99,
    get_price: function() { return this.price; }
  }
};
customObject = {
  __proto__: {
    price: 29.99,
    get_price: function() { return this.price; }
  }
};
仅删除自己的属性,这意味着直接属于对象而不是其原型的属性

var myObject = {
  price: 20.99,
  get_price: function() {
    return this.price;
  }
};
var customObject = Object.create(myObject);
customObject.price = 19.99; // *
delete customObject.price; // **
console.log(customObject.get_price());
因此,我们删除了price:19.99,现在当我们尝试获取customObject.price时,我们得到了price:29.99,这就是get_price的作用

如果我们调用myObject.get\u price,我们得到myObject.price,而在customObject.get\u price中,我们得到customObject.price。尽管get_price函数实际上位于customObject的内部。_proto_u,但函数调用中的特殊变量this指的是调用函数的对象,而不是函数所属的对象


如果您认为同一个函数可以属于不同的对象,那么这是有意义的。

Javascript中的所有对象都有一个uu proto_u_u属性,称为其原型。它与某些函数的prototype属性不同,原因我将不讨论

原型链是在请求时查找某个名称的顺序。例如,dog.bark将在dog中搜索“bark”,但如果dog没有“bark”属性,则它将在dog中搜索“bark”。uu proto_u等等,直到它到达原型链的末端

使用括号语法{}声明的新对象将对象作为其原型

var myObject = {
  price: 20.99,
  get_price: function() {
    return this.price;
  }
};
var customObject = Object.create(myObject);
customObject.price = 19.99; // *
delete customObject.price; // **
console.log(customObject.get_price());
函数Object.createsome返回一个新对象,该对象的原型为某个对象。有些可以为null以创建没有原型的对象

var myObject = {
  price: 20.99,
  get_price: function() {
    return this.price;
  }
};
var customObject = Object.create(myObject);
customObject.price = 19.99; // *
delete customObject.price; // **
console.log(customObject.get_price());
在*中,对象customObject如下所示:

customObject = {
  price: 19.99,
  __proto__: {
    price: 29.99,
    get_price: function() { return this.price; }
  }
};
customObject = {
  __proto__: {
    price: 29.99,
    get_price: function() { return this.price; }
  }
};
在**中,对象customObject如下所示:

customObject = {
  price: 19.99,
  __proto__: {
    price: 29.99,
    get_price: function() { return this.price; }
  }
};
customObject = {
  __proto__: {
    price: 29.99,
    get_price: function() { return this.price; }
  }
};
仅删除自己的属性,这意味着直接属于对象而不是其原型的属性

var myObject = {
  price: 20.99,
  get_price: function() {
    return this.price;
  }
};
var customObject = Object.create(myObject);
customObject.price = 19.99; // *
delete customObject.price; // **
console.log(customObject.get_price());
因此,我们删除了price:19.99,现在当我们尝试获取customObject.price时,我们得到了price:29.99,这就是get_price的作用

如果我们调用myObject.get\u price,我们得到myObject.price,而在customObject.get\u price中,我们得到customObject.price。尽管get_price函数实际上位于customObject的内部。_proto_u,但函数调用中的特殊变量this指的是调用函数的对象,而不是函数所属的对象

如果您认为相同的函数可以属于不同的对象,那么这是有意义的