Kevin Meredith
Kevin Meredith

Reputation: 41939

Understanding `Boolean.constructor` and `Boolean.constructor(true)`

Looking at the Boolean.constructor:

var bool = true;
var booleanObj = new Boolean(true);
console.log ('typeof bool', typeof bool);             # returns 'boolean'
console.log ('typeof booleanObj', typeof booleanObj); # returns 'object'

The following line returns: function Function() { [native code] }. How can I see the native code?

console.log('Boolean.constructor', Boolean.constructor);

Lastly, how can I get

var y = Boolean.constructor(true);
console.log('typeof y', typeof y); # returns function

Then, printing y gives: y: function anonymous() { true }. How can I extract true?

console.log('y:', y);

http://jsfiddle.net/9YxkE/

Upvotes: 0

Views: 37

Answers (1)

SLaks
SLaks

Reputation: 888303

Boolean is a function.

Its constructor property is the constructor for all functions; namely, the Function function.

The native code is part of the Javascript engine and is typically written in C++.
If you want to, you can explore the source code for V8 or SpiderMonkey.

true is the body of the function you created by calling the Function constructor.

Upvotes: 1

Related Questions