Quiz1
Is there a "class" in JavaScript?
Everything is the object
In JavaScript, in addition to basic data (Undefined, Null, Boolean, Number, String), the rest are objects (object).
In fact, objects in JavaScript are a collection of data and functionality. For example, we know:
var foo = new Function ("alert (' Hello world! ')");
Foo ();
It is visible that Foo is a function and an object. Again, for example:
function foo () {
//do something
}
foo.data = 123;
foo["data2"] = "Hello";
alert (foo.data);
alert (FOO.DATA2);
Functions can also add properties like objects.
Construction of objects
Generally we use constructors to build objects, but without constructors we also have the means to build the objects we want:
function Creatperson (__name, __sex, __age) {return
{
name: __name, sex
: __sex, age
: __age,
Get:function (__key) {
alert (This[__key]);
}} var bob = Creatperson ("Bob", "male",);
Bob.get ("name"); Bob
bob.get ("sex"); Male
Bob.get ("Age"); 18
But that's not enough, and I want the method to be shared. For example, when I create a Tom object with this function, the Get function is created again, which obviously wastes my memory.