将函数合并到现有对象中

huangapple go评论58阅读模式
英文:

Merging a function into an existing object

问题

如果一个已经存在的对象 obj

const obj = {
  i: 0,
  foo(){ 
    console.log(this.i) 
    this.i +=1
  }
}

obj.foo() // => 0

是否可以将一个函数合并到它里面:

// 应该将以下函数代码添加到 `obj` 中
function obj(){
  this.foo()
}

// 合并后以下两个函数都应该可用:
obj.foo() // => 1
obj() // => 2
英文:

If one has an existing object obj:

const obj = {
  i: 0,
  foo(){ 
    console.log(this.i) 
    this.i +=1
  }
}

obj.foo() // => 0

Is it possible to merge a function into it:

// the following function code should be added to `obj`
function obj(){
  this.foo()
}

// after the merge the following should both work:
obj.foo() // => 1
obj() // => 2

答案1

得分: 1

我不确定obj函数如何被合并到obj对象中,如果你想要单独调用它的话。然而,你可以使用一个绑定函数来代替。

此外:你不能同时拥有一个名为obj的常量和一个名为obj的函数。因此,我将函数名称更改为fn

const obj = {
  i: 0,
  foo() {
    console.log(this.i);
    this.i += 1;
  }
};

function fn() {
  this.foo();
}

const bound = fn.bind(obj);

bound();
obj.foo();
bound();

编辑

现在我想到了,你不需要fn函数,因为你可以使用const bound = obj.foo.bind(obj); 并且达到完全相同的效果:

const obj = {
  i: 0,
  foo() {
    console.log(this.i);
    this.i += 1;
  }
};

const bound = obj.foo.bind(obj);

bound();
obj.foo();
bound();
英文:

I am not sure how the obj function is merged into the obj object if you want to call it standalone. However, you could use a bound function instead.

Besides: You cannot have a constant obj and a function obj that share the same name. I therefor changed the function name to fn:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const obj = {
  i: 0,
  foo() {
    console.log(this.i);
    this.i += 1;
  }
};

function fn() {
  this.foo();
}



const bound = fn.bind(obj);



bound();
obj.foo();
bound();

<!-- end snippet -->

EDIT

Now that I think of it, you don't need the fn function since you can use const bound = obj.foo.bind(obj); and achieve exactly the same:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const obj = {
  i: 0,
  foo() {
    console.log(this.i);
    this.i += 1;
  }
};

const bound = obj.foo.bind(obj);

bound();
obj.foo();
bound();

<!-- end snippet -->

答案2

得分: 0

使用 Object.assign 可以将函数合并到对象中。我将 obj() 更改为 baz() 以使其更清晰。

const obj = {
  i: 0,
  foo() {
    console.log(this.i);
    this.i += 1;
  }
};

function baz() {
  this.foo();
}

Object.assign(obj, { baz });
obj.foo() // => 0
obj.baz() // => 1

但是,当您调用 baz() 时,它会引发错误,因为它无法理解 baz() 中的上下文 this。因此,您需要使用 obj 作为上下文来调用 baz()

baz.call(obj); // => 2
英文:

You can merge function to object using Object.assign. I changed obj() to baz() to make it more clear.

const obj = {
  i: 0,
  foo() {
    console.log(this.i);
    this.i += 1;
  }
};

function baz() {
  this.foo();
}

Object.assign(obj, { baz });
obj.foo() // =&gt; 0
obj.baz() // =&gt; 1

However when you call baz(), it will throw error because it doesn't understand the context this in baz(). So you need to call baz() with obj as the context

baz.call(obj); // =&gt; 2

答案3

得分: -1

以下是已经翻译好的内容:

有多种方法可以向对象添加属性,如:

  1. 使用点表示法,
  2. 使用方括号 [] 表示法,
  3. 使用 defineProperty(),
  4. 使用展开运算符,
  5. 使用 Object.assign()
const obj = {
  i: 0,
  foo(){ 
    console.log(this.i) 
    this.i +=1
  }
}

const newObj = {...obj, obj(){console.log("ok")}}
newObj.obj()

obj['bar'] = function(){
    console.log("bar")
}

obj.bar()
英文:

there are multiple ways to add property to an object like :

1.using dot notation, 
2.using bracket [ ] notation, 
3.using defineProperty(), 
4.using spread operator, 
5.using Object.assign() 

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const obj = {
  i: 0,
  foo(){ 
    console.log(this.i) 
    this.i +=1
  }
}

const newObj = {...obj, obj(){console.log(&quot;ok&quot;)}}
newObj.obj()

obj[&#39;bar&#39;] = function(){
    console.log(&quot;bar&quot;)
}

obj.bar()

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月15日 12:29:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定