Jump to content

Javascript class


xenophobia

Recommended Posts

function MyClass(){
this.filename = "Hello World";

this.myfunc = function(){
	var my = function(){
		alert(this.filename);
	}

	my();
}
}

 

So this is the class. Then I create an object of this class:

o = new MyClass();
o.myfunc();

 

Will alert undefined. The problem is the variable scope. It cant get the 'filename' within the class.

This will work:

this.myfunc = function(){
     alert(this.filename);
}

 

It can't access the second level of in the function. Any helps?

Link to comment
https://forums.phpfreaks.com/topic/50758-javascript-class/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.