【问题标题】:Is there a way to force a javascript element to redefine itself based on how it was originally defined?有没有办法强制 javascript 元素根据最初定义的方式重新定义自己?
【发布时间】:2019-12-31 05:45:30
【问题描述】:

我在玩对象和构造函数之类的东西,我想知道是否有一种方法可以根据最初定义的方式将值绑定到变量。我有以下代码:

打字稿

let cr = "create",
  ap = "apply",
  $this = {
    set: (prop, value) => {
      this[prop] = value;
    }
  };
function creator() {
  this.$ = (array: Object[]) => {
    array.forEach((kp: Object) => {
      let key = Object.keys(kp)[0];
      let val = kp[Object.keys(kp)];
      $this[key] = val;
      creator.create(key, { value: val });
    });
  };
  this.apply = (...objects: Object[]) => {
    objects.forEach((obj: Object) => {
      creator.call(obj);
    });
  };
}
function create(obj) {
  function createValues(arr) {
    let instance: Object = new obj();
    let vals: any[] = [];
    arr.forEach(name => {
      vals.push(instance[name]);
    });
    return vals;
  }
  let names: string[] = Object.getOwnPropertyNames(new obj());
  let values: string[] = createValues(names);
  return combineArrays(names, values);
}
function combineArrays(arr1, arr2): { $?: any } { // the question mark removes an IDE error
  let newObj: Object = {};
  arr1.forEach(prop => {
    newObj[prop] = arr2[arr1.indexOf(prop)];
  });
  return newObj;
}
Object.prototype.create = function(prop, options) {
  return Object.defineProperty(this, prop, options);
};
create(creator).$([
  { hi: "hi" }, 
  { bye: $this["hi"] } // this is the important stuff
]);

我想知道是否有办法在$this 变量的set 函数中检测它是如何设置 的,从而确定该值是否已更改,所以它是价值应该,如果这有任何意义?假设你有这个:

let $this = {
  set: function(prop, value) {
    this[prop] = value;
  } 
}
let name = 'Ezra';
$this['name'] = name;
// then :
name = 'Bob';
// change $this.name too, so then:
console.log($this.name);
// >> 'Bob'

我相信这称为数据绑定,但我不确定如何在不创建无数代理的情况下做到这一点。

【问题讨论】:

    标签: javascript typescript data-binding


    【解决方案1】:

    您所描述的并不是真正的“数据绑定”,而是pass-by-reference。在您的示例中,您希望 name 的更新反映在 $this['name'] 中。这只有在您将 reference(或 pointer)传递给变量时才有可能。

    但是,在这种情况下,变量是 string,而 JavaScript 中的字符串是 immutable

    没有字符串方法改变它们操作的字符串,它们都返回新的字符串。原因是字符串是不可变的——它们不能改变,我们只能创建新的字符串。

    所以,一步一步地看你的例子:

    这会创建一个名为“Ezra”的新字符串,并分配一个名为name 的变量来引用该字符串。

    let name = 'Ezra';
    

    这会在$this 中创建(或设置)一个名为'name' 的属性,该属性引用name 中的字符串(即'Ezra')。

    $this['name'] = name;
    

    这将创建一个名为'Bob' 的新字符串并将其存储在名为name 的变量中。变量已经存在。这不会改变之前保存在那里的值。相反,name 正在更新以指向新的引用。

    // then :
    name = 'Bob';
    

    然而,如果你要传递一个对象,你会注意到你实际上可以改变它。这是因为对象是通过引用传递的,您可以改变该引用处的值

    例如:

    // Create a new value that stores an object, with a property 'firstName'
    let name = { firstName: 'Ezra' };
    
    // Assign myObject to $this['name']. Now $this['name'] and name both point to the same reference.
    $this['name'] = name;
    
    // Change the value in the object pointed-to by name
    name.firstName = 'Bob'
    
    console.log($this['name'].firstName); // <- This will output 'Bob'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-25
      • 2020-12-27
      • 1970-01-01
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      • 2019-11-28
      • 1970-01-01
      相关资源
      最近更新 更多