最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if an object has a user defined prototype? - Stack Overflow

programmeradmin0浏览0评论

Simply put can I check if an object has a user defined prototype?

Example;

var A = function() {};

var B = function() {};

B.prototype = {

};

// Pseudocode
A.hasUserPrototype(); // False
B.hasUserPrototype(); // True

Is this possible?

Simply put can I check if an object has a user defined prototype?

Example;

var A = function() {};

var B = function() {};

B.prototype = {

};

// Pseudocode
A.hasUserPrototype(); // False
B.hasUserPrototype(); // True

Is this possible?

Share Improve this question edited May 16, 2013 at 21:10 GriffLab asked May 16, 2013 at 20:47 GriffLabGriffLab 2,1664 gold badges20 silver badges22 bronze badges 7
  • 1 What do you call user defined? A prototype that was not created by you? – Geeky Guy Commented May 16, 2013 at 20:50
  • 1 You're only setting prototype as a property of those objects, there's no prototypal inheritance that will occur with the example you've provided. – zzzzBov Commented May 16, 2013 at 20:51
  • @zzzzBov I know this. But still in my example I have modified the prototype by assigning it an empty object, is there a way to check this? – GriffLab Commented May 16, 2013 at 20:53
  • @GriffLab, you have not modified "the prototype" you have only modified an attribute on the object that happens to be called "prototype". – zzzzBov Commented May 16, 2013 at 20:57
  • 1 @GriffLab: "Prototype" is a very specific term in JavaScript. In your example, you have not changed the prototype of the object, you just have created a totally arbitrary property with the name prototype. Only the prototype property of functions is treated in a special way. If you just want to test whether a property exists or not, you should phrase the question differently (and I'm sure it's a duplicate then). – Felix Kling Commented May 16, 2013 at 20:57
 |  Show 2 more comments

3 Answers 3

Reset to default 13

Assuming you want to find out whether an object is an instance of a custom constructor function, you can just compare its prototype against Object.prototype:

function hasUserPrototype(obj) {
    return Object.getPrototypeOf(obj) !== Object.prototype;
}

Or if you maintain the constructor property properly:

function hasUserPrototype(obj) {
    return obj.constructor !== Object;
}

This would also work in browsers which don't support Object.getPrototypeOf.

But both solutions would return true also for other native objects, like functions, regular expressions or dates. To get a "better" solution, you could compare the prototype or constructor against all native prototypes/constructors.


Update:

If you want to test whether a function has a user defined prototype value, then I'm afraid there is no way to detect this. The initial value is just a simple object with a special property (constructor). You could test whether this property exists (A.prototype.hasOwnProperty('constructor')), but if the person who set the prototype did it right, they properly added the constructor property after changing the prototype.

Felix King accurately addressed the issue of inheritance, so I will address the concept of existing properties instead

If you're simply trying to check for the presence of a property named prototype on an object, you can use:

a.hasOwnProperty('prototype')

This will return true for:

a = {
    //the object has this property, even though
    //it will return undefined as a value
    prototype: undefined 
};

This assumes that the object is not being treated as a hashmap, where other properties, such as hasOwnProperty have been set, otherwise, a safer way of checking for the presence of a property is:

Object.prototype.hasOwnProperty.call(a, 'prototype')

This can be turned into a generic function as:

has = (function (h) {
    "use strict";
    return function (obj, prop) {
        h.call(obj, prop);
    };
}(Object.prototype.hasOwnProperty));

and used as:

has(a, 'prototype');

For A object prototype will be undefined:

typeof A.prototype == "undefined" // true
typeof B.prototype == "undefined" // false
发布评论

评论列表(0)

  1. 暂无评论