Javascript 不管是否存在';这是全球性的

Javascript 不管是否存在';这是全球性的,javascript,Javascript,var construct=function(){ var self=这个; 变量视图={ 姓名:“自我” }; self.vm={ 附件:附件,, 视图:视图 } 附加函数(){ 警报('view.name:'+view.name); 视图={ 姓名:“附件” }; 警报('view.name(附加后):'+view.name); 警报('self.vm.view.name:'+self.vm.view.name); 警报('this.view.name:'+this.view.name);

var construct=function(){
var self=这个;
变量视图={
姓名:“自我”
};
self.vm={
附件:附件,,
视图:视图
}
附加函数(){
警报('view.name:'+view.name);
视图={
姓名:“附件”
};
警报('view.name(附加后):'+view.name);
警报('self.vm.view.name:'+self.vm.view.name);
警报('this.view.name:'+this.view.name);
}
self.vm.attached();
返回self.vm;

}()
要将此归结为一个简单的示例:

var a={name:'one'};
var b={view:a};
//看看b.view的观点
console.log(b.view);
//换换衣服再看一眼
a={name:'two'};

console.log(b.view)将其归结为一个简单的示例:

var a={name:'one'};
var b={view:a};
//看看b.view的观点
console.log(b.view);
//换换衣服再看一眼
a={name:'two'};

控制台日志(b.视图)
函数attached()
中,基本上是将
视图
变量重新指定给新创建的对象。您没有更改现有对象,该对象仍然存在。由于
self.vm.view
保存对
视图的初始值的引用,因此在执行
self.vm.view.name
时,您将看到这些值

如果要更改原始对象中的值,必须执行以下操作

view.name = "attached";

函数attached()
中,基本上是将
视图
变量重新指定给新创建的对象。您没有更改现有对象,该对象仍然存在。由于
self.vm.view
保存对
视图的初始值的引用,因此在执行
self.vm.view.name
时,您将看到这些值

如果要更改原始对象中的值,必须执行以下操作

view.name = "attached";

这是因为
view
是一个指针

var construct = function() {
  var self = this;

  // this create a new object, with the property name being "self"
  // and "view" as a pointer to that object
  var view = {
    name: "self"
  };

  self.vm = {
    attached: attached,
    // here the property "view" of the "vm" object points to the variable "view", which itself points to the original object (the one with name="self")
    view: view
  }

  function attached() {
    // the pseudo-global "view" object is called, it reaches out to the object it points to and gives us the name: "self"
    alert('view.name: ' + view.name);
    // now we are changing "view" to point to a NEW object, with the property name="attached"
    view = {
      name: "attached"
    };
    // view points to the new object, it's name is "attached"
    alert('view.name(after attached): ' + view.name);
    // the "view" property of self.vm still points to the original object, the one with the name "self"
    alert('self.vm.view.name: ' + self.vm.view.name);
    alert('this.view.name: ' + this.view.name);
  }

  self.vm.attached();

  return self.vm;
}()
再看看这个问题:。也许它比我更能解释事情


这是一个棘手的问题。

这是因为
view
是一个指针

var construct = function() {
  var self = this;

  // this create a new object, with the property name being "self"
  // and "view" as a pointer to that object
  var view = {
    name: "self"
  };

  self.vm = {
    attached: attached,
    // here the property "view" of the "vm" object points to the variable "view", which itself points to the original object (the one with name="self")
    view: view
  }

  function attached() {
    // the pseudo-global "view" object is called, it reaches out to the object it points to and gives us the name: "self"
    alert('view.name: ' + view.name);
    // now we are changing "view" to point to a NEW object, with the property name="attached"
    view = {
      name: "attached"
    };
    // view points to the new object, it's name is "attached"
    alert('view.name(after attached): ' + view.name);
    // the "view" property of self.vm still points to the original object, the one with the name "self"
    alert('self.vm.view.name: ' + self.vm.view.name);
    alert('this.view.name: ' + this.view.name);
  }

  self.vm.attached();

  return self.vm;
}()
再看看这个问题:。也许它比我更能解释事情


这是一个棘手的问题。

您是否也可以写入处于警报状态的值。“…因此self.vm.view将指向一个新对象。”这是不正确的。您是否也可以写入处于警报状态的值。“…因此self.vm.view将指向一个新对象。”这是不正确的。