2

In this answer, I instructed the questioner not to overwrite the native Object function in Javascript, because I thought that it would mess up the object creation process.

But then, I thought about it, and figured that it's pretty unlikely that all objects are actually created using this function, along with the new keyword.

For example, is it used when objects are created with a literal notation (var a = {...})? Or does it simply do this = {}? Is it possible to actually assign values to this at all?

I saw this question, that is similar, and apparently Object behaves differently when used without the new keyword... How is the function actually implemented?

3
  • ''+Object' is "function Object() { [native code] }" ...so it's not implemented in JS. Commented Jul 21, 2012 at 22:05
  • What does it mean that something is "native code"? That only browsers partake in implementing it, no outside spec or something? Commented Jul 21, 2012 at 22:13
  • 1
    a quick test (using Firefox) shows that u can't replace the Object function. You can modify Object.prototype and that will work with objects created with the literal notation ( var x = {};). Also it is usualy a bad idea to mess with Object.prototype because it can break a lot of 3-rd party scripts, so your advice was correct. Commented Jul 21, 2012 at 22:15

3 Answers 3

2

You get the same object with literal notation as you get with functional notation. Proof:

> ({}).__proto__ === new Object().__proto__
true
> new Object().__proto__ === Object.prototype
true

It means that objects on the left side and on the right side are created from the same prototype, which is Object.prototype

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

Comments

2

Object function is there to try something to be an object, If you won't pass any argument with it, It will construct an object with null, therefore it's an empty object.

var a = new Object(); // a is {}
var b = new Object("1"); // b is an object which store 1 as a string.
/* 
  JS console says:
  String
    0: "1"
    length: 1
*/
var c = new Object(1); // c is an object which store 1 as an integer.
/* 
  JS console says:
  Number
  //There is no other property or method here.
*/

I tried it without new keyword, and nothing changed. All objects are same as above ones.

Hope this resolve your curiosity.

Comments

1

You can overwrite Object in Chrome/V8 and bad things happen if you do. Typing the following gets these responses.

> Object
 function Object() { [native code] }
> Number
 function Number() { [native code] }

Looking at Number.prototype we can see a full set of methods and Object as Number's prototype:

Number
constructor: function Number() { [native code] }
toExponential: function toExponential() { [native code] }
toFixed: function toFixed() { [native code] }
toLocaleString: function toLocaleString() { [native code] }
toPrecision: function toPrecision() { [native code] }
toString: function toString() { [native code] }
valueOf: function valueOf() { [native code] }
__proto__: Object
  __defineGetter__: function __defineGetter__() { [native code] }
  __defineSetter__: function __defineSetter__() { [native code] }
  __lookupGetter__: function __lookupGetter__() { [native code] }
  __lookupSetter__: function __lookupSetter__() { [native code] }
  constructor: function Object() { [native code] }
  hasOwnProperty: function hasOwnProperty() { [native code] }
  isPrototypeOf: function isPrototypeOf() { [native code] }
  propertyIsEnumerable: function propertyIsEnumerable() { [native code] }
  toLocaleString: function toLocaleString() { [native code] }
  toString: function toString() { [native code] }
  valueOf: function valueOf() { [native code] }

But if we overwrite Object

Object = {}

Number's prototype goes a bit wonky:

Number.prototype
   > Number

  ...empty...

As Object is the root of the hierarchy there's a bit of a paradox if you reassign it to another object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.