eloquent js snippet 9

function Rabbit(type) { this.type = type; } var killerRabbit = new Rabbit("killer"); var blackRabbit = new Rabbit("black"); console.log(blackRabbit.type); //attention ca commence! //d'abord on va utiliser ce fameux prototype //des instance //--------------------------------------------------- Rabbit.prototype.teeth = "small"; console.log(killerRabbit.teeth); //ensuite on va donner a une instance une propriete //homonyme killerRabbit.teeth = "long, sharp, and bloody"; console.log(killerRabbit.teeth); //puis on va constater que ca n'a affecte ni les autres //instances, ni le prototype (ca revient au meme). console.log(blackRabbit.teeth); console.log(Rabbit.prototype.teeth); //-------------------------------------------------- Rabbit.prototype.dance = function() { console.log("The " + this.type + " rabbit dance a jig", '\n\n'); }; killerRabbit.dance(); //------------------------------------------------- /*Mais ajouter des proprietes au travers de l'Object..protoype presente un danger car ces proprietes deviennent enumerables*/ console.log("le Rabbit ", Object.getOwnPropertyNames(Rabbit), "\n"); console.log("objet prototypal de la fonction Rabbit ", Object.getOwnPropertyNames(Rabbit.prototype),'\n\n'); console.log(Object.getOwnPropertyNames(killerRabbit)); console.log('\n\n', "pere du killerRabbit ", Object.getOwnPropertyNames(killerRabbit.__proto__),'\n\n'); console.log("grand-pere du killerRabbit", Object.getOwnPropertyNames(killerRabbit.__proto__.__proto__),'\n\n'); console.log("arriere-grand-pere du killerRabbit ", Object.getOwnPropertyNames(killerRabbit.__proto__.__proto__.__proto__),'\n\n');
prototypes 7paradoxe

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.