Jump to content

Uncaught TypeError: Cannot set property 'xxx' of undefined


The Little Guy

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.