2014-02-17 123 views
2

我有兩個班的jQuery例如:在jquery中獲取對象名稱?

function a(){ 

this.init = function(a){} 

} 

function b(){ 

this.init = function(a){} 

} 

兩個類具有this.init() method.I遇到這樣的情況,我有兩個類的對象,我想調用的init()的b類方法,我怎麼能知道自定義對象的名字,這樣我可以方便地調用b類的init()方法類似

if (current_object == typeof b) 
    current_object.init() 
+2

這是什麼了使用jQuery做? – mpen

回答

5

使用的instanceof,

if (current_object instanceof b) 
    current_object.init() 
+0

O gr8 thanx很多它爲我工作。 –

1

可以使用對象

var obj = new b(); 
console.log(obj.constructor == b); 
console.log(obj.constructor == a); 

在行動constructor屬性:http://jsfiddle.net/9j3HJ/

b()對象

if (current_object.constructor == b) 
    current_object.init();