grep
grep

Reputation: 4016

Javascript class call

In a few examples of code I have seen the following when calling a class:

var foo = new Foo.bar();

In which it seems a method is being called upon instantiation. How would a class structure be set up to accommodation this? When I try to directly access a method when calling a new class like this I get an error: call to anonymous function.

Thanks!

Upvotes: 1

Views: 183

Answers (1)

Joe
Joe

Reputation: 82614

var Foo = {};
Foo.bar = function () {
   this.variable = "something";
}

var foo = new Foo.bar();
foo.variable === "something"; // true

Foo is a pseudo-namespace, but really it's just an object. bar is an anonymous function that in this case saves a var name variable in it's scope.

Upvotes: 6

Related Questions