admin管理员组文章数量:1429844
I have a basic understanding of instanceof in JavaScript, testing if the left hand side object "is of" the right hand side object type. The following 2 examples help me understand that...
var demo1 = function() {};
demo1.prototype = {
foo: "hello"
};
var demo2 = function() {
var pub = {
bar:"world"
};
return this.pub;
};
var obj1 = new demo1();
var obj2 = new demo2();
console.log(obj1 instanceof demo1); //returns true
console.log(obj2 instanceof demo2); //returns true
But on this 3rd example, I get false and I don't understand why....
var o = {}; // new Object;
o.toString(); // [object Object]
console.log(o instanceof toString); //returns false
Thanks for any help in understanding whats going on. Also...is is possible to make the 3rd example true?
Thanks again
I have a basic understanding of instanceof in JavaScript, testing if the left hand side object "is of" the right hand side object type. The following 2 examples help me understand that...
var demo1 = function() {};
demo1.prototype = {
foo: "hello"
};
var demo2 = function() {
var pub = {
bar:"world"
};
return this.pub;
};
var obj1 = new demo1();
var obj2 = new demo2();
console.log(obj1 instanceof demo1); //returns true
console.log(obj2 instanceof demo2); //returns true
But on this 3rd example, I get false and I don't understand why....
var o = {}; // new Object;
o.toString(); // [object Object]
console.log(o instanceof toString); //returns false
Thanks for any help in understanding whats going on. Also...is is possible to make the 3rd example true?
Thanks again
Share Improve this question asked Oct 15, 2012 at 14:03 klewisklewis 8,43816 gold badges69 silver badges112 bronze badges 4-
Your
demo2
function is screwed up. There is nothis.pub
, so it will returnundefined
, and when called with thenew
operator this will result in an empty object (beeing aninstanceof demo2
at least) – Bergi Commented Oct 15, 2012 at 14:08 - It returns true for me, I'm using an example based off of T.J. Crawford's blog here - blog.niftysnippets/2010/03/anonymouses-anonymous.html – klewis Commented Oct 15, 2012 at 14:12
-
1
Yes,
obj2 instanceof demo2
istrue
for me, too. Still, your constructor is useless and could be replaced by an empty function. I can't see how yours came from any example in that very good article. – Bergi Commented Oct 15, 2012 at 14:18 - Bergi I didn't post example 2 as a best practice for patterns, I know that. It's based on a response from a blog ment on that article. That's all. I plan on using standard patterns to achieve my goals. Thanks for the heavy critique on staying away from the style mentioned above. And yes its a great article. – klewis Commented Oct 15, 2012 at 14:23
3 Answers
Reset to default 4toString
does not cause a type change of o
. It just returns a string representation of the object, without altering it. So, o
is still a simple object and no instanceof String
.
var o = {}; // new Object object
var ostring = o.toString(); // "[object Object]"
typeof o; // object
typeof ostring; // string - this is a primitive value, not an object; so that
ostring instanceof String; // is false
var stringobj = new String(ostring); // new String object
typeof stringobj; // object
stringobj instanceof String; // true!
o instanceof Object; // also true!
See also MDN reference for the String
constructor.
o
is an object
; toString
is a function
. They are different types in JavaScript.
alert(typeof(o)); //"object"
alert(typeof(toString)); //"function"
JavaScript makes a distinction between objects and functions. Therefore, that's why you get false
returned in your example.
With an object literal like
var o = {}; // new Object;
you create an object whose prototype is Object
. Testing with instanceof
will not yield any useful information. The only parison that will yield true is
o instanceof Object
本文标签: Understanding instanceof with JavaScriptStack Overflow
版权声明:本文标题:Understanding instanceof with JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745515386a2661506.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论