-1
  1. Is there any possibility of anyObject.constructor property being null or undefined(especially when ConstructorFunc.prototype is not modified/overwritten)?
  2. var date = new Date(); console.log(date.constructor); // logs "Date()". ok, fine.
    var data = new Array(1, 2, 3); console.log(data.constructor); // it logs something like [ undefined ]. What is it and why is not Array().

TIA

3
  • 1
    var data = new Array(1, 2, 3); console.log(data.constructor); shows me function Array() { [native code] }. Which platform are you using? See the following fiddle. Commented Jul 27, 2012 at 18:57
  • hmm.. yes, more confusion. If I try the same code in firebug console, it logs [ undefined]. Commented Jul 27, 2012 at 19:02
  • 2
    @Arjun: Don't rely too heavily on console output. It's an interpretation, and is prone to errors and misinterpretation. Commented Jul 27, 2012 at 19:08

2 Answers 2

2
  1. Yes, you can manually overwrite the .constructor property of a constructor function's prototype object.

  2. It seems that the constructor property has been changed. Normally you'd probably see something like function Array() { [native code] } instead of [ undefined ].

One thing you can do to verify is...

console.log(typeof [].constructor);

It should give you "function". If it gives you "object", then it's been changed.


Don't trust console output

It seems from a comment that you're testing in Firebug.

As a general rule do not put too much trust in console logging. Consoles are addons to the environment, and must interpret what they've been given to log. Sometimes the interpretation is misleading.

If you get odd results, then perform other tests...

console.log(Array);          // [ undefined ] ...huh???
console.log([].constructor); // [ undefined ] ...huh???

typeof [].constructor; // Firebug still gives "function"

[].constructor === Array; // Firebug returns true

So you can see that even though Firebug gave an odd interpretation of the function itself, it doesn't change the fact that it is still the expected Array constructor.

Sign up to request clarification or add additional context in comments.

Comments

0
  1. The constructor property is writable. Hence anyObject.constructor can be explicitly set to undefined or null (this will create a new property on anyObject and won't modify the constructor property of the prototype). You may also change the constructor property of the prototype to null or undefined so that it's reflected on every instance of the function. By default it'll never be null or undefined.

  2. It shows me function Array() { [native code] } as expected. Since you don't get the same result perhaps Array.prototype.constructor has been modified by some third party code. You should check that.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.