//ELOQUENT JS object snippet 3.

//ELOQUENT JS object snippet 3. //la fonction fait appel a un objet qu'elle n'est pas et on en fait une //des valeurs de propriete de cet objet function speak(line) { console.log("The " + this.type + " rabbit says '" + line + "'"); } // var whiteRabbit = {type: "white", speak: speak}; var fatRabbit = {type: "fat", speak: speak}; whiteRabbit.speak("Oh my ears and whiskers, " + "how late it's getting!"); fatRabbit.speak("I could sure use a carrot right now."); //L'utilisation de la methode 'apply'pour la fonction // 'speak' nous dit de quel instance des objets // qui la possedent on veut s'occuper pour la // propriete nommee 'type' contenue et thissee dans // la fonction et nous dit quel argument on veut lui donner. //est entre crocthets et guillemete comme dans // la notation crochetee de name-value speak.apply(fatRabbit, ["Burp!"]); //la methode call nous permet de ne pas nous referer a une // instance connue et nous pouvons former un objet singleton // avec une propriete nommee type comme celle thissee // et l'argument de la fonction n'est pas crochete. speak.call({type: "old"}, "Oh my.");

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.