Hello I've a question when creating a class with functions.

e.g.

function constructor() {
	this.myFunc = function() {
		return "Hello World";
	}
}

do I have to write the function in that way? this.funcName = function() for every function in that class?

Because when I tried to write it in this way instead

function constructor() {
	function() myFunc{
		return "Hello World";
	}
}

It does not work if it is bundled like function in a function. When I create an object of constructor, and then try to call the myFunc function.

Try doing it this way:

var isConstructor, myfunc;

isConstructor = function() {
   myfunc = function() {
   alert("Hello World!");
   }; 
return { myfunc : myfunc };
 }();

window.onload = isConstructor.myfunc;

Hello I've a question when creating a class with functions.

e.g.

function constructor() {
	this.myFunc = function() {
		return "Hello World";
	}
}

do I have to write the function in that way? this.funcName = function() for every function in that class?

Because when I tried to write it in this way instead

function constructor() {
	function() myFunc{
		return "Hello World";
	}
}

It does not work if it is bundled like function in a function. When I create an object of constructor, and then try to call the myFunc function.

In Javascript functions are first class members meaning a function is a type just like an int or a string. Using this.varname = function(){}; is assigning a variable to this.varname, the variable type just happens to be a function. If you use function funcname(){} it declares it in the current scope and is destroyed once you leave that scope. IE., if you declare it inside another function you can't access it outside that function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.