2014-02-17 113 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();