ciso
ciso

Reputation: 3050

Why does the getPrototype method call result in an error?

If every object inherits eventually from the highest level Object and that highest level Object has the getPrototypeOf() function/method, why does the following code produce an error?

obj = {};
proto = obj.getPrototypeOf(obj);

Error: Object doesn't support property or method 'getPrototypeOf'

When I look in IE10's (F12 developer tools) Locals tab, it says obj has a prototype, and that prototype has methods, one of which is isPrototypeOf.

Here is my complete html:

<!DOCTYPE html>
<html>
<head>
<script>
obj = {};
proto = obj.getPrototypeOf(obj);
</script>
</head>
<body>
</body>
</html>

Please note: I'm specifically asking why it shows up as a method under the prototype for obj, yet produces an error (if it's suppose to only be a method of Object, but not the inherited prototype)? I would post a screenshot of it, but my reputation is too low since I'm new.

Upvotes: 1

Views: 134

Answers (3)

sunysen
sunysen

Reputation: 2351

try

obj = {};
proto = Object.getPrototypeOf(obj)

Upvotes: 0

Matti Virkkunen
Matti Virkkunen

Reputation: 65126

It's because getPrototypeOf is not a method on all objects, it's a method on the... Object object. Use Object.getPrototypeOf(obj).

Upvotes: 0

Musa
Musa

Reputation: 97672

getPrototypeOf is on the Object object and not on instances of an object, try

obj = {};
proto = Object.getPrototypeOf(obj);

Upvotes: 2

Related Questions