Feature Request: Have a way to determine if an object is wrapped by Zepto #349

diazruy opened this Issue Dec 2, 2011 · 10 comments


None yet

8 participants


In jQuery, executing the following returns true:

$('<div></div>') instanceof jQuery //=> true
$('<div></div>') instanceof $ //=> true

This is useful in Jasmine for example when writing custom matchers where you want to test to see if an object has been wrapped by Zepto. However, in Zepto the following behaviour occurs:

$('<div></div>') instanceof Zepto //=> false
$('<div></div>') instanceof $ //=> false

Is it possible to allow comparable behaviour for Zepto?


Why not add a property to $.fn and test for that property on the object?

$.fn.isZepto = true

$('<div></div>').isZepto //=> true

There's no reason why it shouldn't. I'd need to look closer at the code to understand why not.
The inheritance method works by replacing the _____proto_____ property of the element array. A simple test:

function Zepto() {};
var arr = []; arr._____proto_____ = new Zepto;
(arr instanceof Zepto) // = true!


There is an internal Z() function that I think $() objects respond true to. I tried adding Z as a function to $, as $.Z and in that

$('<div></div>') instanceof $.Z // => true

This seemed to have other ramifications though


You're right - it is poor design. I'd consider this a bug in need of fixing more than a feature.


"instanceof Zepto" would make for better branding =)


This worked for me, both on Zepto and jQuery. Of course, I could be grossly misunderstanding something here. ;)

$('<div></div>') instanceof $.fn.constructor


$('<div></div>').__proto__ === $.fn


Having instanceof Zepto or instanceof $ would be ideal. This is a solid alternative:

var selectorProto = $().prototype; // Save to var so that func only calls once.

function isSelector(ukn){// non-chainable version
    return ukn instanceof Object && selectorProto === ukn.prototype; // boolean

// OR

function isSelector(){ // chainable version
    return this instanceof Object && selectorProto === this.prototype; // boolean

With the latest master (not in v0.8), you can do:

$.zepto.isZ(object)   // => returns true if it is a Zepto collection
@madrobby madrobby closed this Apr 5, 2012

I tried to run FancyBox with zepto (just for fun). But it uses obj instanceof $ check.
To get this check working, we need $.prototype = $.fn . That's all.
@madrobby what do you think about including this into your library ?


Great @danyaPostfactum !
Added $.prototype = $.fn and all plugins using instanceof works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment