My goal is to create a master_script.js. A handler file mainly used in development.
A subsection of this file is dedicated to customizing the prototypes of the foundation objects:
- String
- Number
- Array
- Object
- Date
- Function
- Boolean
So as I'm writing out my code blocks...
/********************************************************/
Object.defineProperty(
Number.prototype,'len',
{get:function(){
str=this.toString()
return str.length
}}
)
/********************************************************/
n=123
o={123:'abc',abc:123}
/********************************************************/
console.log(
'n ⇒ ',n,
'\n n.len ⇒ ',n.len,
'\n n.__proto__ ⇒ ',n.__proto__
)
/********************************************************/
Works fine!
- when I expand
n.__proto__
in the console it displays thelen: (...)
property and it's getter functionget len: function (){
. - and when I type
n.
into the console it is clearly visible on the list of properties|methods.
The problems start when I configure the same setup for the o
variable:
/********************************************************/
Object.defineProperty(
Number.prototype,'len',
{get:function(){
str=this.toString()
return str.length
}}
)
/******************************/
Object.defineProperty(
Object.prototype,'len',
{get:function(){
cnt=0
for(i in this) cnt++;
return cnt
}}
)
/********************************************************/
n=123
o={123:'abc',abc:123}
/********************************************************/
console.log(
'n ⇒ ',n,
'\n n.len ⇒ ',n.len,
'\n n.__proto__ ⇒ ',n.__proto__
)
/******************************/
console.log(
'o ⇒ ',o,
'\n o.len ⇒ ',o.len,
'\n o.__proto__ ⇒ ',o.__proto__
)
/********************************************************/
The first thing I notice is that when I now type n.
or o.
into the console the popup list of properties|methods no longer contains a reference to the len
property, but if I type in the full extension n.len
or o.len
I get the desired result of 3
or 2
...
So I know the code is legit but I need the console popup as there is about 40 different properties & methods for each of the 7 foundation objects...
So I wrote a test block to try & identify the problem:
/********************************************************/
Object.defineProperty(
Object.prototype,'dis',
{get:function(){return this}}
)
/******************************/
Object.defineProperty(
Number.prototype,'len',
{get:function(){return this.length}}
)
/********************************************************/
o={123:'abc',abc:123}
n=123
e=document.createElement('option')
/********************************************************/
console.log(
'o ⇒ ',o,
'\n o.__proto__ ⇒ ',o.__proto__
)
/******************************/
console.log(
'n ⇒ ',n,
'\n n.__proto__ ⇒ ',n.__proto__
)
/******************************/
console.log(
'e ⇒ ',e,
'\n e.__proto__ ⇒ ',e.__proto__
)
/********************************************************/
Immediately after expanding the .__proto__
in the console for o
, n
and now e
, I noticed that all 3 had been given the dis
property I had originally tried to assign to solely the o
object.
Then I cracked it: Number.prototype
itself is an object, ergo it inherits the dis
property from the Object.prototype
How can I append the dis
property solely to the o
object without it being inherited by n
from Object.prototype
?