The Little Guy Posted January 26, 2011 Share Posted January 26, 2011 I am trying to do oop javascript, but I am getting an error with this: function JEffect(){ var props = new Array('x', 'y'); this.prototype.transition = function(obj, prop, begin, finish, drration){ if(!this.in_array(prop, props)){ alert('not found'); } } var in_array = function(needle, haystack){ for(var k in haystack){ if(k == needle){ return true; } } return false; } } <script type="text/javascript"> var effect = new JEffect(); effect.transition('', 'm', 1, 2, 2); </script> The error message says: Uncaught TypeError: Cannot set property 'transition' of undefined What does that mean and what do I do? Quote Link to comment https://forums.phpfreaks.com/topic/225752-uncaught-typeerror-cannot-set-property-xxx-of-undefined/ Share on other sites More sharing options...
Alex Posted January 26, 2011 Share Posted January 26, 2011 You have a few problems there. You can't set properties through "this.prototype", and you don't define properties with "var" just use the this keyword. This won't throw any errors, but I'm not entirely sure what you're attempting to do: function JEffect(){ this.props = new Array('x', 'y'); this.in_array = function(needle, haystack){ for(var k in haystack){ if(k == needle){ return true; } } return false; } } JEffect.prototype.transition = function(obj, prop, begin, finish, drration){ if(!this.in_array(prop, this.props)){ alert('not found'); } } var effect = new JEffect(); effect.transition('', 'm', 1, 2, 2); Because you're adding the in_array method in the constructor I'm guessing you want to add that to the JEffect prototype as well, but I left it as you had it. Quote Link to comment https://forums.phpfreaks.com/topic/225752-uncaught-typeerror-cannot-set-property-xxx-of-undefined/#findComment-1165543 Share on other sites More sharing options...
The Little Guy Posted January 26, 2011 Author Share Posted January 26, 2011 Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/225752-uncaught-typeerror-cannot-set-property-xxx-of-undefined/#findComment-1165562 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.