I'm confused why var obj = {}; or var obj = new Object(); then obj.getOwnPropertySymbols is undefined??
- 10,027
- 9
- 55
- 83
-
Which IDE are you using? – Akash Agarwal Jun 18 '16 at 03:20
-
this is in chrome, not sure what version – neaumusic Jun 18 '16 at 03:20
-
How exactly are you doing it in chrome? – Akash Agarwal Jun 18 '16 at 03:21
-
1`CMD+Option+J` or `Control+Shift+J` on PC – neaumusic Jun 18 '16 at 03:21
-
browsers still have some deprecated methods: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineGetter__ – omarjmh Jun 18 '16 at 03:23
3 Answers
Object itself has some methods defined, such as Object.keys. These aren't defined on Object.prototype, so they're not available on object instances.
- 1,386
- 10
- 20
-
dang, yea i don't know how this wasn't obvious to me, very strange to not put helper functions on the prototype – neaumusic Jun 18 '16 at 03:23
-
Nah it's not. If that happened you'd be unable to define a method called `keys()` for example, it would be a nightmare for backwards compatibility – Dan Oct 21 '17 at 19:46
Constructors have two separate kinds of methods:
Prototypical methods
Defined as properties of
Constructor.prototype, the are only available on instances.Static methods
Defined as properties of
Constructoritself, the are only available on the constructor.
ES6 classe make it clear
class Constructor {
static foo() { return 123; }
bar() { return 456; }
}
Constructor.foo(); // 123
Constructor.bar; // undefined
new Constructor().foo; // undefined
new Constructor().bar; // 456
Your case is special, however: the static methods of the Object constructor seem to include the prototypical methods. But that's because Object inherits from Object.prototype.
The prototypical chains are like
Objectinstanceinstance -> Object.prototype -> nullObjectconstructorObject -> Function.prototype -> Object.prototype -> null
- 274,082
- 63
- 437
- 513
Technically, Object (the constructor) is not of the type object, it's a class/function depending on what term you want to using with JS. Because it's a class, it has methods on it that are not used in instances of that object. IE: they're not on the returned objects prototype.
In your Chrome console, try this:
var obj = {};
typeof obj; // "object"
typeof Object; // "function"
Look into the way class prototyping works in JS when you get a chance.
To clarify a bit, when you run new Object() it returns something with the type of object that has the necessary methods on it.
I think this is a good reference of what I'm talking about: JavaScript - The Good Parts: Function prototypes vs Object prototypes
You can see the same behavior with Number and String. The types of both are actually function, but once they're created, you get one of the type of number or string, each with their relevant prototype methods.
The bottom line is the constructor is not the same as the returned value.
- 1
- 1
- 3,682
- 2
- 20
- 43
-
-
-
that sentence is why I Dv'd and now its gone, relax dude - I meant the same thing Oriol did – omarjmh Jun 18 '16 at 03:44
-
1@JordanHendrix It's all good man. I just like to have feedback with DV, I put a lot of thought into my answers, so if it's wrong, I'd like to know why. In your case, I understand, still not sure what the DVs are for. – Jacques ジャック Jun 18 '16 at 03:46

