简体   繁体   English

调用 function 时的返回值 object

[英]Return valueOf object when called as a function

I'm creating a specific class in which I'd like to return the value of the class when it is called standalone, rather than returning the entire class model.我正在创建一个特定的 class ,我想在它被称为独立时返回 class 的值,而不是返回整个 class349DBAF35E68305 class 的值。

I've seen other people ask similar questions and seen recommended solutions using the valueOf and toString methods, but those methods both require writing the class as a string somewhere or calling it in some form of computation.我见过其他人提出类似的问题并看到使用valueOftoString方法的推荐解决方案,但是这些方法都需要将 class 作为字符串写入某处或以某种形式的计算调用它。 I'd like my class to work in such a way where when I call the class, even in a simple console log by itself, it returns its value, without any other details about the other properties and methods available.我希望我的 class 以这样的方式工作,当我调用 class 时,即使在一个简单的控制台日志中,它也会返回它的值,而没有关于其他可用属性和方法的任何其他详细信息。 I'm sure some people will say this is counterintuitive and probably not a good idea, but this is my ideal use case, and this is exactly how the Date() object works, so I feel this must be possible somehow.我相信有些人会说这是违反直觉的,可能不是一个好主意,但这是我的理想用例,这正是Date() object 的工作原理,所以我觉得这一定是可能的。

When you create a new instance of the Date object, there are plenty of methods available (eg getMonth(), getFullYear(), setTime(), toJSON(), toDateString(), etc. ), but if you just call an instance of the date object without any methods or properties specified, it appears to return the date, unless this is just the browser console assisting developers by displaying some form of the stringified date, so they don't need to call .toString() to get the string value by default.当您创建Date object 的新实例时,有很多方法可用(例如getMonth(), getFullYear(), setTime(), toJSON(), toDateString(), etc. ),但如果您只是调用一个实例日期 object 没有指定任何方法或属性,它似乎返回日期,除非这只是浏览器控制台通过显示某种形式的字符串化日期来帮助开发人员,所以他们不需要调用.toString()来获取默认情况下的字符串值。

 const now = new Date(); console.log(now); // -> "2021-04-26T14:44:30.466Z" console.log(now.toString()); // -> Mon Apr 26 2021 10:44:59 GMT-0400 (Eastern Daylight Time) console.log(now.valueOf()); // -> 1619448336448

Would returning a "default" property like this be possible?可以返回这样的“默认”属性吗? Please keep in mind, I have already read similar questions here on StackOverflow and really scoured the platform and not seen any that answer this questions specifically.请记住,我已经在 StackOverflow 上阅读了类似的问题,并且真的搜索了平台,但没有看到任何专门回答这个问题的。

The bottom line is that you don't have much control over what the console API does.底线是您无法控制console API 的功能。 When you hand it an object, in order to be helpful it will give you the object contents to explore.当您将 object 交给它时,为了有所帮助,它会为您提供 object 内容供您探索。 Of course the console code is familiar with native JavaScript types, so it might do something special with things like Date instances because, again, it's trying to be helpful.当然,控制台代码熟悉原生 JavaScript 类型,因此它可能会对 Date 实例之类的东西做一些特别的事情,因为它再次试图提供帮助。 (That "helpfulness" causes a lot of confusion and there are hundreds if not thousands of questions here that can be traced to people not understanding what the console is trying to do.) (这种“有用性”引起了很多混乱,这里有成百上千个问题可以追溯到人们不理解控制台试图做什么。)

So you can add .toString() and .valueOf() methods to your classes, and take advantage of them for debugging etc. by either calling the methods explicitly or using them implicitly, as in:因此,您可以将.toString().valueOf()方法添加到您的类中,并通过显式调用方法或隐式使用它们来利用它们进行调试等,如:

console.log("Current status: " + myObject);

The + concatenation operator will implicitly call the .toString() method. +连接运算符将隐式调用.toString()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM