Creating a new object by specifying a handle to its definition

In this Udacity game development video , the instructor mentions that Javascript allows us to create an object by specifying a handle to its definition. It is then said that in order for this "definition of an overloaded object to update the hash table with a pointer to the class definition."

I know what a hash table is, a pointer, an overloaded method, and a factory template, but I cannot understand this mysterious statement or the rest of the explanation.

+4
source share
2 answers

A "hash table" is simply a more convenient way of saying "regular Javascript object." What an instructor means to “describe its definition” is another way of saying “a function that acts as a constructor for a class”.

Ultimately, what does it mean in the statement you mentioned:

each redefined entity definition will update the hash table with a pointer to the class definition

:

  • All "subclasses" of Entity will register their design function in one common hash file / object, using the key, which is the value type.
  • This allows you to get a constructor function (in other words, an newon call function that will return an instance of this object) for any type by referring to gGameEngine.factory[type].

, , , , gGameEngine.factory , , .

, , JSON, , , - :

var typeConstructor = gGameEngine.factory(tileSpec.type),
    instance;

if (typeConstructor) {
    instance = new(typeConstructor)(tileSpec /* or whatever params */);
}

, 1 , .

?

+4

, , , , // . , , , , "handle" "hash table".

var someClass = function () {
  // stuff
}

var containingObject = {};
containingObject["someClass"] = someClass;
// same thing as
containingObject.someClass = someClass;

, containingObject.

var classInstance = new containingObject["someClass"]()
// or
var classInstance = new containingObject.someClass()
+1

All Articles