Skip to content
This repository

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

Closed
diazruy opened this Issue · 9 comments

7 participants

Ruy Diaz Thomas Fuchs Alex Wilson Oliver Morgan Kevin Dente Ryan Van Etten danyaPostfactum
Ruy Diaz

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?

Alex Wilson

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

$.fn.isZepto = true

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

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!

Ruy Diaz

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
case:

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

This seemed to have other ramifications though

Oliver Morgan

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

Deleted user

The content you are editing has changed. Reload the page and try again.

"instanceof Zepto" would make for better branding =)

Sending Request…

Attach images by dragging & dropping or selecting them. Octocat-spinner-32 Uploading your images… Unfortunately, we don't support that file type. Try again with a PNG, GIF, or JPG. Yowza, that's a big file. Try again with an image file smaller than 10MB. This browser doesn't support image attachments. We recommend updating to the latest Internet Explorer, Google Chrome, or Firefox. Something went really wrong, and we can't process that image. Try again.

Kevin Dente

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

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

Alternately

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

Ryan Van Etten

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
}
Thomas Fuchs
Owner

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

$.zepto.isZ(object)   // => returns true if it is a Zepto collection
Thomas Fuchs madrobby closed this
danyaPostfactum

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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.