- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 js 新手。我有一个我不理解的 if
条件。你能告诉我这个 if 条件的作用吗?
Object.prototype.toString.call(currentFruit) === "[object Date]"
你能解释一下吗?在下面提供我的代码:
setcurrentFruit: function (fruitName, currentFruit) {
WorklistStorage.set(fruitName, currentFruit, false);
},
getcurrentFruit: function (fruitName) {
var currentFruit = unescapeJSON(WorklistStorage.get(fruitName, false));
if (currentFruit == "undefined" || typeof currentFruit == "undefined" || Object.prototype.toString.call(currentFruit) === "[object Date]") {
var date = new Date();
currentFruit = date.toString();
wholeQueue.setcurrentFruit(fruitName, currentFruit);
//console.log("poppppp");
}
currentFruit = new Date(currentFruit);
return currentFruit;
},
最佳答案
让我们分解一下;
Object
; native JavaScript 对象的引用。Object.prototype
;对 Object 的所有实例继承的属性的引用。Object.prototype.toString
;所有对象的 native toString
方法。Function.prototype.call
;使用选定的 this
调用函数的方法因此 Object.prototype.toString.call(currentFruit)
正在调用 toString
上所有对象 的 native currentFruit
。如果在 currentFruit.toString()
上定义或继承了另一个 toString
,则这可能与 currentFruit
不同。
Object.prototype.toString
返回 [object X]
形式的 String,其中 X
是 this
的类型,因此将 [object Date]
与 ===
进行比较是在询问 "Is currentFruit
a 约会?”
为什么做这个检查比 typeof
更有用?因为 typeof
通常只会返回 "object"
,这通常没有帮助。
instanceof
呢?这将是 true
,如果您正在检查的东西也继承自您正在测试的东西,例如,x instanceof Object
通常是 true
,这也不总是有帮助。
您可能认为类似的替代方法是测试对象 的构造函数。 x.constructor === Date
。这有一组不同的问题,例如如果 throw
是 undefined 或 null 则 x
ing 错误,因此需要更多检查等。但如果您正在工作,它会更有帮助对于非本地构造函数,toString
将简单地给出 [object Object]
。
综上所述,您需要考虑在您所使用的环境下此测试是否永远正确。目前没有标准的 JSON 表示日期。
关于javascript - Object.prototype.toString.call(currentFruit) === "[object Date]",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18885015/
数据框有一个字符串类型的日期列 '2017-01-01' 它被转换为 DateType() df = df.withColumn('date', col('date_string').cast(Dat
这个问题在这里已经有了答案: What is "x && foo()"? (5 个答案) 关闭 8 年前。 我在 bootstrap-datepicker.js 文件中遇到过这个。 作者在_setD
我有一个数据库 utc 字符串,我正在传递到 Date(attrs.endDate),然后通过 new Date() 减去当前的 utc 日期,但我无法得到它来为我提供 2 个 utc 日期的正确差异
这个问题在这里已经有了答案: how to determine if 2 dates object equals each other? [duplicate] (3 个答案) 关闭 6 年前。 我
这个问题已经有答案了: How can I convert "/Date(1399739515000)/" into date format in JavaScript? (3 个回答) 已关闭 8
根据MDN ,我们只能将以下类型的参数传递给 Date 构造函数: new Date(); new Date(value); // Unix timestamp new Date(dateString
我从表中获取所有项目: endDate >= 现在 endDate 为 NULL published 等于 1。 这是我所拥有的,但它给了我 0 个项目: $items = Items::orderB
此查询需要很长时间才能完成。当我将 WHERE 子句设置为 new_dl >= '2014-01-01' 时,查询大约需要 6 分钟才能浏览大约 3 个月的数据。现在不知道为什么这个应该从 12 个月
我有一个正在为项目开发的小型 Java 程序,它使用 JavaMail 从指定的 URI 中提取用户的收件箱,然后开始处理消息。 在 Outlook 中,属性菜单中有一个功能可以设置邮件的到期日期,它
我想在获取 Date.getHours()、Date.getMinutes() 和 Date.getSeconds() 的值后格式化输出>. 这是一条漫长的路: var dt = new Date()
我发现java.text.DateFormat有两种格式化日期的方法。一种是采用 Date 参数,另一种是采用 Object 参数。我检查了DateFormat源代码,似乎他们调用了不同的内部方法。
我有两个对象,p4 和 p5,它们都具有 Date 属性。在某些时候,构造函数工作正常: p4.setClickDate(new Date(System.currentTimeMillis() - 8
我是使用 Sequelize 和 Node.js 的新手,但我的代码中存在日期比较问题。 User.findOne({ where: { resetToken: passwordToken,
我正在使用一个名为 fullcalendar 的 jquery 日历。当用户单击某一天时,他们将被发送到另一个页面以创建该天的事件。单击的日期作为 date 提供。然后通过下面的函数运行将其转换为 U
我有一个列表列表,每个列表中都有整数值,代表 8 年期间的日期。 dates = [[2014, 11, 14], [2014, 11, 13], ....., [2013, 12, 01]
我有两个表: 首先是TimeValues(示例) time | value 12/28/18 | 5.6 01/03/19 | 5.6 01/04/19 | 5.6 01/09/19 | 5.
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
像这样实例化的日期对象: new Date("2011-12-13") 返回一个认为自己是星期一的日期对象: Date {Mon Dec 12 2011 16:00:00 GMT-0800 (PST)
我需要选择入住日期和退房日期在指定日期范围之间的房价。这些费率根据其条件单独命名。房费取决于所选日期。这是我的代码: rate_eb rate_name rate_starts rat
我有 [Int64:[String:String]] 其中 Int64 是时间戳。如何检测和删除 [String:String] 中的参数之一是 ["name"] = "test" 并重复多次的同一天
我是一名优秀的程序员,十分优秀!