in The morning to see The JavaScript weekly introduction "[3], find an article inside (Extending JavaScript - The Right Way, feel is good, with everyone sharing, The translation of this article is not to translate word for word, said very easy to understand as much as possible.
if (! String. Prototype. Capitalize)
{
String. The prototype. Capitalize=function ()
{
return this. Slice (0, 1). The toUpperCase () + this. Slice (1) toLowerCase ();

}}

the above code can be used normally, but if there are the following code somewhere:

var strings="yay";
the for (I in strings). The console log (I + ":" + strings [I]);

we get the result is like this:
0: y
1: a 2:

y capitalize: function () {return this. Slice (0, 1). The toUpperCase () + this. Slice (1) toLowerCase (); }
this is obviously not what we want as a result, the output is the way we add the reason is that the way we increase the enumerable attribute to true by default.
we can simply enumerated attribute set to false (enumerable) to avoid this problem, use defineProperty method for function extension:

if (! String. Prototype. Capitalize)
{
Object. DefineProperty (String. The prototype, the 'capitalize,
{
value: the function ()
{
return this. Slice (0, 1). The toUpperCase () + this. Slice (1) toLowerCase ();
},
enumerable: false
});
}

now we run this code:

var strings="yay";
the for (I in strings). The console log (I + ":" + strings [I]);

we get the result:
0: y:
1
2 a: y
note that use cycle without output does not represent does not exist, we can use the following code to view the definition:

var strings="yay";
the console. The log (strings. Capitalize)

will output:

the function () {return this. Slice (0, 1). The toUpperCase () + this. Slice (1) toLowerCase (); }

extension JavaScript function in this way is more flexible, we can use this way to define our own objects, and set some default values.
here are a few other extension methods, you can use in your own project:
String. PxToInt ()
convert "200px" such String to number 200:

if (! String. Prototype. PxToInt)
{
Object. DefineProperty (String. The prototype, the 'pxToInt,
{
value: the function ()
{
return parseInt (enclosing the split ('px) [0]).
},
enumerable: false
});
}

String. IsHex ()
to determine whether a String of hexadecimal representation, such as "#CCC" or "#CACACA"

if (! String. Prototype. IsHex)
{
Object. DefineProperty (String. The prototype, the 'isHex,
{
value: the function ()
{
return this. The substring (0, 1)==' #'&&
(enclosing length==4 | | this. Length==7) &&
/^ [0-9] a - fA - F + $/. The test (enclosing slice (1));
},
enumerable: false
});
}

String. The reverse ()
String reversal:

if (! String. The prototype. The reverse)
{
Object. DefineProperty (String. The prototype, the 'reverse',
{
value: the function ()
{
return this. The split (' '). The reverse () join (");
},
enumerable: false
});
}

String. The wordCount ()
statistical word number, separated by Spaces

if (! String. The prototype. The wordCount)
{
Object. DefineProperty (String. The prototype, the 'wordCount,
{
value: the function ()
{
return this. The split (''). Length;
},
enumerable: false
});
}

String. The htmlEntities ()
HTML tags such as <And >For special characters

if (! String. The prototype. The htmlEntities)
{
Object. DefineProperty (String. The prototype, the 'htmlEntities,
{
value: the function ()
{
the return String (this). The replace (/&/g,' &'). The replace (//g, '>'). The replace (//g, '"' ");
},
enumerable: false
});
}

String. The stripTags ()
remove HTML tags:

if (! String. The prototype. The stripTags)
{
Object. DefineProperty (String. The prototype, 'the stripTags,
{
value: the function ()
{
return this. The replace (/<\/? [^ >] + >/gi, ");
},
enumerable: false
});
}

String. The trim ()
removes leading and trailing Spaces:

if (! String. The prototype. The trim)
{
Object. DefineProperty (String. The prototype, the 'trim',
{
value: the function ()
{
return this. The replace (/^ \ s */, ""). The replace (/\ s * $/," ");
},
enumerable: false
});
}

String. StripNonAlpha ()
remove non alphabetic characters:

if (! String. Prototype. StripNonAlpha)
{
Object. DefineProperty (String. The prototype, the 'stripNonAlpha,
{
value: the function ()
{
return this. The replace (//^ A Za - z +/g, "");
},
enumerable: false
});
}

Object. The sizeof ()
the size of the statistical objects, such as {one: "and" two, "and"} to 2

if (! Object. The prototype. The sizeof)
{
Object. DefineProperty (Object. The prototype, the sizeof,
{
value: the function ()
{
var counter=0,
the for (index in this) counter++;
return counter;
},
enumerable: false
});
}


the way to extend the functionality of JS native objects or very good, but unless necessary (projects with many), directly on the original object is not recommended to expand functions, global variables will cause pollution.
in addition, the text of thepxToInt () method is not necessary, JS the parseInt () can be directly to complete this function: parsetInt (" 200px ")===200
htmlEntities approach seems to have a problem, the following offer another:

if (! String. The prototype. The htmlEntities)
{
Object. DefineProperty (String. The prototype, the 'htmlEntities,
{
value: the function ()
{
var div=document. The createElement method (" div ");
if (div. TextContent) {
div. TextContent=this
}
the else {
div. The innerText=this
}
return div. The innerHTML.
},
enumerable: false
});
}


This concludes the body part