b2 et b3 ne déclenchent pas les fonctions du prototype et aucune erreur n'est générée? Comment accomplit-on l'appel de fonctions prototypes à la mode?objet javascript prototype appel
<html>
<head>
<script type="text/javascript">
\t function newObj(){
\t \t this.obj_val= 7;
\t }
\t var trigger_f0 = function(){
\t \t alert("here 0"); \t \t \t \t \t \t \t \t \t \t // trigger FINE! (ok)
\t }
\t newObj.prototype.trigger_f2 = function(){ // no triggering off click event
\t \t alert("here 2");
\t }
\t newObj.prototype.trigger_f3 = function(){ // not triggering off click event
\t \t alert("obj value:" + newObj.obj_val);
\t }
\t var init = function(){
\t \t b3.addEventListener('click', newObj.trigger_f3, false);
\t \t b2.addEventListener('click', newObj.trigger_f2, false);
\t \t b1.addEventListener('click', trigger_f0, false);
\t }
\t window.addEventListener('DOMContentLoaded', init, false);
\t </script>
\t
\t </head>
<body>
<button id="b1">B1</button>
<button id="b2">B2</button>
<button id="b3">B3</button>
</body>
</html>
À un certain moment, vous devez utiliser 'newobj()' construire en fait un objet. En l'état, vous venez de créer une fonction constructeur. Appelez-le avec 'new newObj()' et cela vous donnera un objet connecté à votre prototype. – Pointy
'console.log (newObj.trigger_f3)' –
Oui c'était le problème - mon constructeur était juste cela. var a = new newObj; travaillé! –