function $() {
var elements = new Array();
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}
var anyObject = $( 'id_of_the_object' );
var firstObject = $('id_of_the_object');
var secondObject = $('id_of_another_object');
var manyObjects = $('the_third_object', 'the_fourth_object', firstObject, secondObject);
native_type.prototype.your_new_function = function([arguments]) {
// your JavaScript code here
// in here, the “this” object refers to the variable in which you call the function
}
As an example:
String.prototype.startsWith = function(chars) {
// return true when the substring of the string, starting at 0
// and taking chars.length of length be equal to chars
return (this.substr(0,chars.length) == chars);
}
As strings are case-sensitive in JavaScript, you can even improve the function to make it case-insensitive:
String.prototype.startsWith = function(chars,ignoreCase) {
if(ignoreCase==true) {
return (this.toLowerCase().substr(0,chars.length) == chars.toLowerCase());
} else {
return (this.substr(0,chars.length) == chars);
}
}
Well, I have shown you the way. Now imagine extending the JavaScript objects in multiple ways: string objects with startsWith(), endsWith(), contains(), ltrim(), rtrim(), trim()… imagine the possibilities with the limited and nothing-easy-to-manage JavaScript Date object! This is an endless world waiting to be explored by you. Indisputably, extending native JavaScript objects is a must-have in your common.js.
//---------------------------- get a reference to the list object
var list = document.getElementById( 'id_of_the_list' );
//---------------------------- Create the new option and give it text and value
var newOption = document.createElement( 'option' );
newOption.text = 'the text';
newOption.value = 'the value';
//---------------------------- Add the new option to the list object
list.options.add(newOption);
//---------------------------- get a reference to the list object
var list = document.getElementById( 'id_of_the_list' );
//---------------------------- add the new option
list.options[list.length] = new Option( 'the text', 'the value' );
But even in that way, this is much more readable and pleasant to write:
var list = new ListControl( 'id_of_the_list' );
list.add( 'the text', 'the value' );
As you might deduce from that previous code, we have created a ListControl JavaScript class that acts as a wrapper of the native HTML <select> tag.
function ListControl(id) {
//get a reference to the list control (using, of course, the $() function)
var _base = $(id);
//this function is to show/hide the list control wrapped, or get its visible state
//usage:
//alert(listControlObject.visible());
//listControlObject.visible(false);
this.visible = function(newValue) {
if(newValue==undefined) {
return (_base.style.display != 'none');
} else {
_base.style.display = (newValue==true) ? '' : 'none';
}
}
//this function is to enable/disable the list control wrapped, or get its enabled state
//usage:
//alert(listControlObject.enabled());
//listControlObject.enabled(false);
this.enabled = function(newValue) {
if(newValue==undefined) {
return (!_base.disabled);
} else {
_base.disabled = (!newValue);
}
}
//this function is to get the number of elements contained in the list
this.count = function() {
return _base.options.length;
}
//this function is to clear all the elements from the list
this.clear = function() {
while(_base.options.length > 0) {
_base.options.remove(0);
}
}
//this function is to add a new element to the list. The index argument is optional;
//if not supplied, the new element adds at the end of the list
this.add = function(text,value,index) {
var oItem = document.createElement('option');
oItem.text = text;
oItem.value = value;
if((index==undefined) || (index==null)) {
try {
_base.add(oItem,null);
} catch (e) {
_base.add(oItem,_base.length);
}
} else {
_base.add(oItem,index);
}
}
//this function is to remove an element from the list based on its zero-based position
this.remove = function(index) {
_base.options.remove(index);
}
//this function is to get a concrete element based on its zero-based position. The function
//returns the native Option javasacript object, so we can access to text, value, and so on
this.item = function(index) {
return _base.options[index];
}
//this function is to sort alphabetically the elements in the list. Note that this will only
//work well when elements are text-unique and value-unique
this.sort = function() {
var texts = new Array();
for(var k=0;k<_base.options.length;k++) {
texts[k] = _base.options[k].text;
}
texts.sort();
var values = new Array();
for(var k=0;k<texts.length;k++) {
values[k] = this.valueOf(texts[k]);
}
this.clear();
for(var k=0;k<texts.length;k++) {
this.add(texts[k],values[k]);
}
}
//this function is to retrieve the text that corresponds to a concrete value
this.textOf = function(value) {
for(var k=0;k<_base.options.length;k++) {
if(_base.options[k].value==value) {
return _base.options[k].text;
}
}
return null;
}
//this function is to retrieve the value that corresponds to a concrete text
this.valueOf = function(text) {
for(var k=0;k<_base.options.length;k++) {
if(_base.options[k].text==text) {
return _base.options[k].value;
}
}
return null;
}
//this function is to know when the list contains any element with the specified text
this.containsText = function(text) {
for(var k=0;k<_base.options.length;k++) {
if(_base.options[k].text==text) {
return k;
}
}
return -1;
}
//this function is to know when the list contains any element with the specified value
this.containsValue = function(value) {
for(var k=0;k<_base.options.length;k++) {
if(_base.options[k].value==value) {
return k;
}
}
return -1;
}
}
Please refer to the comments in the listing for detailed documentation of how to use the object.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented:
Could you add that to your article?