【问题标题】:How to add a function as a method to a returned array?如何将函数作为方法添加到返回的数组?
【发布时间】:2019-09-13 19:36:40
【问题描述】:

我有一个打算与new 构造函数一起使用的函数。它应该返回一个值为["Hello World"] 的数组,并包含函数作为方法。这是一个示例/演示(对不起,如果它有点难看):

var my_object = function () {
  this.foo = function (data) { // alert and add new item
    alert(data);
    this.push(data);
  };

  this.bar = function () { // alert and remove last item
    alert(this.pop());
  };

  this.baz = function (stuff) { // replace last item by running previous methods
    this.bar();
    this.foo(stuff);
  };

  var a = "Hello World";
  return [a];
};

var arr = new my_object(); // ["Hello World"]

正如预期的那样,arr 的值为["Hello World"]。但是,以下代码会产生错误:

arr.foo('some-text'); // "TypeError: arr.foo is not a function"

其他两个函数出现相同类型的错误。在不更改 Array.prototype、在函数外部创建方法或删除使用 new my_object() 初始化的能力的情况下,我能做些什么来完成这项工作?

注意:如果可能,请不要包含使用 jQuery 或其他外部库的答案。

【问题讨论】:

  • this 不是您从函数返回的值,因此您声明的任何方法都将被丢弃而不使用。
  • 因为你返回一个数组,..数组没有名为'foo'的方法
  • 所以你想从数组继承并添加新方法?
  • 这背后的设计理念是什么? Arrays类型。您正在使用具有面向对象方法的函数,但要从中返回一个数组。
  • 这可能对 OP 有所帮助 -> github.com/wesbos/es6-articles/blob/master/…

标签: javascript arrays function object methods


【解决方案1】:

正如cmets中提到的,这个链接有一些有价值的信息-> https://github.com/wesbos/es6-articles/blob/master/54%20-%20Extending%20Arrays%20with%20Classes%20for%20Custom%20Collections.md

使用它,您的代码可以像这样修改。 ->

当然这是ES6,在ES5 天内无法扩展数组..

在 Chrome instanceof 中,当映射按预期工作时,如果这是必需的,则不确定转译的代码是否可以在这里工作。例如,如果您单击此 sn-p 上的使用 Babel 预设,您将看到它失败,因此如果您希望它在旧浏览器上工作,即使已转译也可能是一个问题。

class my_object extends Array {
  foo(data) {
    alert(data);
    this.push(data);
  }

  bar() {
    alert(this.pop());
  }

  baz(stuff) {
    this.bar();
    this.foo(stuff);
  }

  constructor () {
    super("Hello World");
  }
};

var arr = new my_object(); // ["Hello World"]
arr.foo("some-text");

console.log(arr);

【讨论】:

  • 这可能比以前的答案更好(即对我的代码的修改更少)。谢谢!
【解决方案2】:

如果您不使用任何Babel 或其他可以安全转换代码的方法,请使用inheritance and the prototype chain。 JavaScript 默认不是面向对象的编程,而是假装 OOP。

在构造的情况下,您返回 ["Hello World"],它是 array不是 my_object 的实例。您可以返回 this 而不是 [a]

此外,您没有任何可以引用的数组。当您使用 this 时,您在本地对象上工作,并且您可能有一个 TypeError: this.push is not a function

我改进了你的情况。看看什么是返回,我使用 this 并引用 data 数组。

你好,李子!

改进的代码:

var my_object = function () {
  this.data = [];
  this.foo = function (data) { // alert and add new item
    alert(data);
    this.data.push(data);
  };

  this.bar = function () { // alert and remove last item
    alert(this.data.pop());
  };

  this.baz = function (stuff) { // replace last item by running previous methods
    this.bar();
    this.foo(stuff);
  };

  var a = "Hello World";
  return this;
};

var arr = new my_object(); // [object Object] - a my_object instance
console.log(arr)
arr.foo('some-text');

【讨论】:

  • 您的解决方案应该张贴在这里而不是外部网站。此外,它不起作用 - 它返回 一个对象 而不是一个数组。
  • 谢谢!我需要进行一些编辑才能使用我的其余代码,但它会很好用。
  • @VLAZ 我编辑了我的答案,我会记住这一点。您不能返回数组并处理实例方法。或者你正在返回数组或对象,但是从构造函数返回数组有什么意义呢?
  • @plumthedev 你可以返回一个数组。更准确地说是数组的子类。或者你也可以只做return [],但是构造函数是一个奇怪的选择。无论如何,我对 OP 在问题中表达的意思表示反对,即希望 arr 真正成为一个数组。 OP 从未回答 cmets 来指定它是否需要是文字数组、数组的子类、类似数组,或者......显然,根本不是数组,因为这是选择的解决方案。
猜你喜欢
  • 1970-01-01
  • 2016-04-29
  • 1970-01-01
  • 2012-10-11
  • 2020-03-25
  • 2022-01-14
  • 1970-01-01
  • 2017-08-18
  • 1970-01-01
相关资源
最近更新 更多