gpt4 book ai didi

javascript - 在 Backbone.fetch() 中寻找一些代码的解释

转载 作者:行者123 更新时间:2023-12-03 12:34:15 24 4
gpt4 key购买 nike

我经常阅读源代码作为学习编程的好资源。我在查看 Backbone.Model 的 fetch() 方法时遇到了这个问题,我希望有人能解释一下。

这里是 fetch() 方法:

// ---------------------------------------------------------------------
// Fetch the model from the server. If the server's representation of the
// model differs from its current attributes, they will be overriden,
// triggering a `"change"` event.
fetch: function(options) {
options = options ? _.clone(options) : {};
if (options.parse === void 0) options.parse = true;
var success = options.success;
options.success = function(model, resp, options) {
if (!model.set(model.parse(resp, options), options)) return false;
if (success) success(model, resp, options);
};
return this.sync('read', this, options);
}

我的问题是,if 条件 if (options.parse === void 0)... 的意义何在? void 0 只是评估为“undefined”,如果属性未定义,这是测试的简写吗?如果是这样,它是否比标准的 (typeof options.parse === 'undefined') 语句有任何优势,我发现后者更具语义和可读性?

最佳答案

除了代码大小和性能之外,还有另一个使用 void 0 的原因。某些浏览器允许为 undefined 分配其他值。这可能导致代码以依赖于浏览器的方式被破坏。

参见:JavaScript `undefined` vs `void 0`

关于javascript - 在 Backbone.fetch() 中寻找一些代码的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21911279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com