Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,207,734 members, 8,000,096 topics. Date: Monday, 11 November 2024 at 09:34 PM

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

Nairaland Forum / Science/Technology / Webmasters / Help! Javascript Gurus. Please Help Me. (781 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)

Wanna Learn Hacking / Apple Buys Nigeria’s Chinedu’s App / Increase And Maintain Your Blog Traffic

(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. 15
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.