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

diazruy commented Dec 2, 2011

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?

arextar commented Dec 2, 2011

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

$.fn.isZepto = true

$('<div></div>').isZepto //=> true
ollym commented Dec 2, 2011

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!

diazruy commented Dec 2, 2011

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

ollym commented Dec 10, 2011

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

ghost commented Dec 22, 2011

"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

ryanve commented Feb 3, 2012

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
madrobby commented Apr 5, 2012

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 ?

pacotole commented Oct 8, 2015

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