J'essaie d'obtenir une meilleure connaissance pratique de JavaScript. J'ai donc acheté le livre "JavaScript the good parts" de Douglas Crockford.JavaScript - Prototype
J'ai du mal à saisir le prototype pour le moment. Tout ci-dessous semble fonctionner dans mon navigateur jusqu'à ce que je tape // PROTOTYPE Exemple. Quelqu'un peut-il y jeter un coup d'oeil pour voir pourquoi je ne peux pas obtenir de sortie de celui-ci. (Ma page est vide sauf si je commente tout le code du prototype)
Nous vous remercions de votre aide.
Barry
var stooge = {
"first-name": "Jerome",
"last-name": "Howard",
"nickname": "J",
"profession" : 'Actor'
};
// below is augmenting
var st = stooge;
st.nickname = "curly";
// st.nickname and nick are the same because both are ref's to the same object
var nick = st.nickname;
document.writeln(stooge['first-name']); //expect Jerome -- this is "suffix" retrieval
document.writeln(st.nickname); //expect "curly" -- this is "notation" retrieval
document.writeln(nick); //expect "curly"
document.writeln(stooge.profession);
//PROTOTYPE EXAMPLE;
if (typeof Object.create !== 'function')
{
object.create = function(o) {
var F = function() {};
F.prototype = o;
return new F();
};
var another_stooge = Object.create(stooge);
another_stooge['first-name'] = 'Barry';
document.writeln(another_stooge['first-name']);
// the below should be inherited from the prototype therefore "Actor"
document.writeln(another_stooge.profession);
Merci Tim, je ne serai pas attrapé par ça à nouveau maintenant :) – CaRDiaK
@ Ferdinand: Nice edit, merci –