Javascript 如何验证是否每次都在创建对象的新实例?
如何检查我是否正在创建Javascript 如何验证是否每次都在创建对象的新实例?,javascript,Javascript,如何检查我是否正在创建对象的新实例 (function($, window, document,undefind){ var Yahoo = { } $.fn.queryTwitter = function(){ this.each(function(){ //$(this).text(Yahoo.name); // How to check if here if its a new instance of
对象的新实例
(function($, window, document,undefind){
var Yahoo = {
}
$.fn.queryTwitter = function(){
this.each(function(){
//$(this).text(Yahoo.name);
// How to check if here if its a new instance of Yahoo object
});
return this;
};
$.fn.queryTwitter.options = {
}
})(jQuery, window, document);
下面的代码每次都会创建Yahoo对象的新实例。但在我的例子中,我使用的是对象文字,我可以使用object.create(yahoo)代码>但如何验证是否每次都在创建对象的新实例?
function Yahoo (){
}
new Yahoo();
如果我能很好地理解你,这将是一种方式:
(function($, window, document,undefind){
var originalYahoo = Yahoo = {};
$.fn.queryTwitter = function(){
this.each(function(){
if(Yahoo !== originalYahoo) alert("New Instance!");
...
});
return this;
};
...
})(jQuery, window, document);
干杯如果是文字,您肯定不会创建任何实例?instance1===instance2
不会工作吗?您所说的“验证”是什么意思?什么是新的实例?比什么新?难道你不知道你是用new
,Object.create
来实例化对象,还是反复使用同一个对象?