Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,161 members, 7,815,057 topics. Date: Thursday, 02 May 2024 at 06:17 AM

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

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

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

(1) (Reply)

Help! Javascript Gurus. Please Help Me. by David(m): 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)

I Want To Blog / Free Website Design Training Live On Nairaland And Be Your Own Boss! / [new] 10+ Google Most Expensive And Highly Paid Keywords

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 12
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.