将函数合并到现有对象中

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

Merging a function into an existing object

问题

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

  1. const obj = {
  2. i: 0,
  3. foo(){
  4. console.log(this.i)
  5. this.i +=1
  6. }
  7. }
  8. obj.foo() // => 0

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

  1. // 应该将以下函数代码添加到 `obj` 中
  2. function obj(){
  3. this.foo()
  4. }
  5. // 合并后以下两个函数都应该可用:
  6. obj.foo() // => 1
  7. obj() // => 2
英文:

If one has an existing object obj:

  1. const obj = {
  2. i: 0,
  3. foo(){
  4. console.log(this.i)
  5. this.i +=1
  6. }
  7. }
  8. obj.foo() // => 0

Is it possible to merge a function into it:

  1. // the following function code should be added to `obj`
  2. function obj(){
  3. this.foo()
  4. }
  5. // after the merge the following should both work:
  6. obj.foo() // => 1
  7. obj() // => 2

答案1

得分: 1

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

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

  1. const obj = {
  2. i: 0,
  3. foo() {
  4. console.log(this.i);
  5. this.i += 1;
  6. }
  7. };
  8. function fn() {
  9. this.foo();
  10. }
  11. const bound = fn.bind(obj);
  12. bound();
  13. obj.foo();
  14. bound();

编辑

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

  1. const obj = {
  2. i: 0,
  3. foo() {
  4. console.log(this.i);
  5. this.i += 1;
  6. }
  7. };
  8. const bound = obj.foo.bind(obj);
  9. bound();
  10. obj.foo();
  11. 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 -->

  1. const obj = {
  2. i: 0,
  3. foo() {
  4. console.log(this.i);
  5. this.i += 1;
  6. }
  7. };
  8. function fn() {
  9. this.foo();
  10. }
  11. const bound = fn.bind(obj);
  12. bound();
  13. obj.foo();
  14. 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 -->

  1. const obj = {
  2. i: 0,
  3. foo() {
  4. console.log(this.i);
  5. this.i += 1;
  6. }
  7. };
  8. const bound = obj.foo.bind(obj);
  9. bound();
  10. obj.foo();
  11. bound();

<!-- end snippet -->

答案2

得分: 0

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

  1. const obj = {
  2. i: 0,
  3. foo() {
  4. console.log(this.i);
  5. this.i += 1;
  6. }
  7. };
  8. function baz() {
  9. this.foo();
  10. }
  11. Object.assign(obj, { baz });
  12. obj.foo() // => 0
  13. obj.baz() // => 1

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

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

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

  1. const obj = {
  2. i: 0,
  3. foo() {
  4. console.log(this.i);
  5. this.i += 1;
  6. }
  7. };
  8. function baz() {
  9. this.foo();
  10. }
  11. Object.assign(obj, { baz });
  12. obj.foo() // =&gt; 0
  13. 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

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

答案3

得分: -1

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

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

  1. 使用点表示法,
  2. 使用方括号 [] 表示法,
  3. 使用 defineProperty(),
  4. 使用展开运算符,
  5. 使用 Object.assign()
  1. const obj = {
  2. i: 0,
  3. foo(){
  4. console.log(this.i)
  5. this.i +=1
  6. }
  7. }
  8. const newObj = {...obj, obj(){console.log("ok")}}
  9. newObj.obj()
  10. obj['bar'] = function(){
  11. console.log("bar")
  12. }
  13. obj.bar()
英文:

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

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

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

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

  1. const obj = {
  2. i: 0,
  3. foo(){
  4. console.log(this.i)
  5. this.i +=1
  6. }
  7. }
  8. const newObj = {...obj, obj(){console.log(&quot;ok&quot;)}}
  9. newObj.obj()
  10. obj[&#39;bar&#39;] = function(){
  11. console.log(&quot;bar&quot;)
  12. }
  13. 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:

确定