I'm trying to define a class named MyClass inside a namespace MyNamespace. The class constructor should accept a single string argument. It should also have a function named sayHello that returns the string passed into the constructor.
The interesting part is that MyClass should only be accessible via the namespace and should not define any extra global variables. Code should not redefine an existing namespace, but should also function if the namespace is not previously defined.
What am I doing wrong here?
var MyNamespace = {
MyClass: (function(string){
function sayHello(){
return string;
}
})()
console.log(new MyNamespace.MyClass('Hello!'));
}
And the link to the fiddle: http://jsfiddle.net/marcusdei/wqvc0k1j/8/
p.s.This is not homework, I'm learning JS for my career. Thanks!
This is probably what you want.
If you are looking for something like a private member you'd do
Result: