Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
jQuery 如何实现其 Deferred 对象,以便new操作符是可选的var x = $.Deferred();?
new
var x = $.Deferred();
这是实现这一目标的模式......
$.Deferred = function() { if ( ! (this instanceof $.Deferred)) { return new $.Deferred; } }
它之所以有效,是因为this在构造函数中设置为新对象。instanceof将告诉您 LHS 操作数是否在其原型链中具有 RHS 操作数。如果此条件不成立,该函数将返回对象的实例化版本。
this
instanceof