₦airaland Forum

Welcome, Guest: Register • Login • With Google • Trending • Recent • New

Stats: 3,344,613 members, 8,512,760 topics. Date: Tuesday, 03 November 2026 at 11:19 PM

Toggle theme

Help! Javascript Gurus. Please Help Me. - Webmasters - Nairaland

Nairaland Forum › Science/Technology › Webmasters › Help! Javascript Gurus. Please Help Me. (912 Views)

1 Reply

Help! Javascript Gurus. Please Help Me. by David(op): 7:23am On Apr 16, 2013
So far, this group has really made me learn js fast. I really appreciate boss. Em. At it again. Is it possible to create a prototype of a prototype.
E.g
Array.prototype.style=
function(){};

style.prototype.textcolor= function(){
document.write("<p style='color:red;'>"+this+"</p>"wink;
}


style.prototype.align=
function(){
document.write("<p align='left'>"+this+"</p>"wink;
}

then using it, we will have something like:

people= ["mary","peter"];

people.style().textcolor().align();

And the result will be:
the names mary, peter in color red and aligned to the left.

Thus style is a prototype of an Array, while textcolor and align are prototypes of style.

please help. Thanks
Re: Help! Javascript Gurus. Please Help Me. by Nobody: 9:47am On Apr 16, 2013
David: So far, this group has really made me learn js fast. I really appreciate boss. Em. At it again. Is it possible to create a prototype of a prototype.
E.g
Array.prototype.style=
function(){};

style.prototype.textcolor= function(){
document.write("<p style='color:red;'>"+this+"</p>"wink;
}


style.prototype.align=
function(){
document.write("<p align='left'>"+this+"</p>"wink;
}

then using it, we will have something like:

people= ["mary","peter"];

people.style().textcolor().align();

And the result will be:
the names mary, peter in color red and aligned to the left.

Thus style is a prototype of an Array, while textcolor and align are prototypes of style.

please help. Thanks
Not Possible and frankly i don't see any use of this, you only extend a prototype of an Object and not an Object's function, in JavaScript Functions are First Class Objects they are treated as objects which is why they can expanded or have properties however that is when called using the new or if it is a Native Object the other function style belongs to Array thus just a simple function and not an object so it cannot be expanded, but if you need to work on an array and deference or chain always return the object Array and that way you can chain another array method, which is how jQuery works. here is one i did for you

//Extend the Array Prototype
Array.prototype.style=function(){
var a=[]; //create a dummy array
//walk over the array and style it pseudo-code
this.forEach(function(item,index,ar){
item+=item + " styled";
a.push(item); // populate array
});
return a; return the array
}
var a=["A","B","C"];
var b=a.style();
console.log( b );
1 Reply

Php/mysql Gurus, Please Help Solve This Problem... • Web Gurus,please Help! • Please Help! Javascript Ebook • 2 • 3 • 4

E-commerce Website Needed • Your Help Is Urgently Needed!!! • Long Waited URL Shortening Service Made In Lagos