[Js] Uso del Prototype in Javascript

I prototipi in javascript consentono di estendere le sue funzionalità base.

Esempio

var a = new String();
 
 String.prototype.UpperCaseOnlyFirstChar= function()
 {
     var ret  = "";
  
     ret = this.substring(0,1).toUpperCase() + this.substr(1,this.length-1).toLowerCase();
  
     return(ret);
 };

 a = "AleX";
 alert(a.UpperCaseOnlyFirstChar());

 

il risultato è "Alex"

posted @ venerdì 27 luglio 2007 19:14

Print

Comments on this entry:

# re: [Js] Uso del Prototype in Javascript

Left by Matteo at 03/08/2007 19:48
Gravatar
@Pietro: tutto corretto, meno il punto 5.

Il ";" dopo la graffa è necessario!! E serve per terminare l'assegnazione di "UpperCaseOnlyFirstChar". Il vero problema è che JavaScript accetta come terminatore di riga anche il semplice a capo (un idea, questa dell'ideatore, veramente malsana).

L'istruzione deve essere terminata, tu lo fai con il terminatore implicito (a capo), ma dovresti farlo con il ";". Questo perchè, una delle ottimizzazioni che di solito si applica ai codici client-side delle applicazioni web è la compressione: la rimozione delle interruzioni di riga (e altro).

Il tuo codice smette di funzionare se lo comprimo. Mentre non ha problemi se lasci il punto e virgola.

m.

# re: [Js] Uso del Prototype in Javascript

Left by nike free run at 19/10/2012 14:47
Gravatar
Amazing nike free story.I know how my 7th grade music teacher changed my life.I'm not in the music business,but my love for music is up there with my love nike free run for my kids.Music has saved my life literally.I owe it all to Mr. Hoover of Cameron Jr.High.

# re: [Js] Uso del Prototype in Javascript

Left by he at 13/11/2014 17:54
Gravatar
started platform networks check your credit score monetary tandem quickly credit report scores evaluating storage a559
Comments have been closed on this topic.
«aprile»
domlunmarmergiovensab
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011