【问题标题】:How to add a default return statement for objects instantiated by constructor functions?如何为构造函数实例化的对象添加默认返回语句?
【发布时间】:2016-09-20 09:06:03
【问题描述】:

考虑一下 JavaScript 的 Date 构造函数如何创建一个在直接引用时返回默认字符串的对象:

var date = new Date();
document.write(date); // Outputs string: Mon May 23 2016 08:48:14 GMT-0400 (EDT)
                      // Expected output: [object Object]

document.write('<br/>', date.getFullYear()); // Can call methods as expected.
document.write('<br/>', date.toString());

根据 MDN 文档:

如果没有提供参数,则构造函数会根据系统设置为当前日期和时间创建一个 JavaScript Date 对象

(强调我的)

Date 构造函数返回一个对象,但是当直接引用该对象时,它返回一个表示当前日期和时间的字符串,而不是像预期的那样表示方法和属性。

如何使用自己创建的对象构造函数实现相同的行为?

例如:

// My CoolObj constructor function
var CoolObj = function () {
  var self = this;
  var i = 0;
  this.coolMethod = function () {
    i += 1;
    return self.coolProperty + ' and increment ' + i;
  };
  this.coolProperty = 'My cool string';
}

var myCoolObj = new CoolObj(); // Instantiate new object from constructor;
                               // Just like `var date = new Date();` above.

document.write(myCoolObj); // Outputs [object Object];
                           // I want to output a string, like Date does.
                           // For example: 'My cool direct-reference string.'

document.write('<br/>', myCoolObj.coolProperty); // Can call properties...
document.write('<br/>', myCoolObj.coolMethod()); // ...and methods as expected.

我希望它在直接引用时返回某种字符串,同时提供正常调用其方法和属性的能力。


更新:

链接的问题确实提供了答案:
Is it possible to override JavaScript's toString() function to provide meaningful output for debugging?

tl;dr: 定义 CoolObj.prototype.toString,它将被 console.logdocument.write 等尝试cast的函数调用在输出之前将对象转换为字符串。

// My CoolObj constructor function
var CoolObj = function () {
  var self = this;
  var i = 0;
  this.coolMethod = function () {
    i += 1;
    return self.coolProperty + ' and increment ' + i;
  };
  this.coolProperty = 'My cool string';
}

// Define a custom `toString` method on the constructor prototype
CoolObj.prototype.toString = function () {
  return 'My cool direct-reference string.';
}

var myCoolObj = new CoolObj(); // Instantiate new object from constructor;
                               // Just like `var date = new Date();` above.

document.write(myCoolObj); // Outputs [object Object];
                           // I want to output a string, like Date does.
                           // For example: 'My cool direct-reference string.'

document.write('<br/>', myCoolObj.coolProperty); // Can call properties...
document.write('<br/>', myCoolObj.coolMethod()); // ...and methods as expected.

【问题讨论】:

  • @mdickin 我不是在寻求在控制台中为对象指定格式的帮助。我正在寻找如何为构造函数实例化的对象添加默认返回语句。
  • @gfullam new Date() 不返回字符串,而是返回一个对象。在其上调用console.log() 会调用Date.toString() 方法。运行typeof new Date() 返回“对象”
  • @mdickin 我知道new Date() 返回一个对象,这就是为什么我引用了 MDN 文档中的说明。正是这一事实让我质疑 Date 在直接引用时返回字符串的独特行为。我已经修改了我的示例,将其与 console.log 的用法分开,以展示我在我想用我自己构造的对象复制的日期构造对象中观察到的内容。
  • @bfmags 这是一个有趣的问题,只与我的问题无关,但它的答案很老套,并引入了严重的性能影响。我正在为我的问题寻找一种公认的“最佳实践”解决方案。

标签: javascript date


【解决方案1】:

我不是在寻求帮助来为控制台中的对象指定字符串格式。

但这就是您所看到的行为。没有“对象的默认返回语句”之类的东西。

考虑一下 JavaScript 的 Date 构造函数如何创建一个在直接引用时返回默认字符串的对象

它没有。它只是创建一个对象。可能是一个特殊的原生 Date 对象,但没有关于“默认字符串”的内容。
确实只有控制台 (console.log) 才能对这些原生 Date 实例进行特殊格式化。

对于任意对象,您无能为力,您所要求的都是不可能的。

【讨论】:

  • 我不是。我已经修改了我的示例,将其与 console.log 的用法分开,以展示我在我想用我自己构造的对象复制的日期构造对象中观察到的内容。
  • 您试图复制的只是console 效果。 JS 中没有对象的这种行为。您所能做的就是编写自己的函数来特别小心地对待自己的对象。
  • document.write(您在新示例中使用)的情况下,它确实将参数转换为字符串(如一些函数) - 然后它是the question that mdickin suggested in the comments 的副本。跨度>
  • 我的意思是像 console.logdocument.write 这样的函数会特别小心地处理传递给它们的对象,而不是对象具有特殊的方法。
  • 是的,完全正确。虽然console.log 仅将Date 对象转换为字符串,并以其他特殊方式(例如DOM 节点)记录其他特殊对象。
猜你喜欢
  • 2015-01-24
  • 1970-01-01
  • 1970-01-01
  • 2016-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-01
  • 2015-03-25
相关资源
最近更新 更多