google chrome - Inheritance in JavaScript with require.js: Illegal invocation -
i'm running problems require.js , prototypal inheritance. here module:
define([], (function() { "use strict"; var player = function() { console.log("constructor of player object"); this.audioobject = new audio(); }; player.prototype = new audio(); return player; })); // define
then try use play method of audio-object way:
define(["jquery", "player"], (function($, player) { "use strict"; /** * controls player **/ var controls = function(player) { $(".play").click(function() { console.log("play"); player.play(); }); }; return controls; })); // define
when clicking on button, chrome throws error: uncaught typeerror: illegal invocation
i have been trying hours now, reading similar problems here of them relate jquery isnt problem here think. (i tried define method called player.prototype.play() myself , worked fine, don't think best way rewrite methods)
this because audio
has hidden "under hood" properties aren't copied prototype when new player
instantiated.
when call player.play()
, play
expects run on audio
instance, it's running on player
instance. theoretically, there shouldn't problem here, because player
should same audio
, since prototype audio
instance. however, audio
host object, means doesn't need play rules of normal javascript objects.
that said, why using audio
instance player
prototype @ all? aside being prone weird behavior (as you're observing), seems totally unnecessary since you're holding audio
object in audioobject
property.
i don't understand design, why not use audioobject
, e.g., call player.audioobject.play()
here instead?
Comments
Post a Comment