I used this article to clarify my own ideas, and hope to clarify my ideas for those who have the same understanding as me. The error code is as follows:
var o = { ...}var obj = new o();
Needless to say, of course, an error is returned. Unfortunately, I used to think that var o = {} is a class called o. Then, I managed it myself. Conclusion:
Var o = {}; equivalent to var o = new Object (); but not equivalent to var o = function (){};
The first two forms o is an Object, an Object class Object. The third form is a function, and more importantly, o is a class.
var o = {};o.oField = "oField";
Equivalent
var o = {oField : "oField"}
The question is: How can attributes be directly defined on objects?
Because o is an Object class Object, o. prototype is undefined, so o. prototype. oField =...
In addition, I wrote two test codes and pasted them together. It can be a note.
// Define the class var Engin = function () {}; // instance attribute Engin. prototype. objectField = "objectField"; // class attribute (static field) Engin. classField = "classField"; // instance method Engin. prototype. objectMethod = function () {document. write ("objectMethod is called <br/>");} // class method (static method) Engin. classMethod = function () {document. write ("classMethod is called <br/>");} // call the instance method new Engin (). objectMethod (); // call the class method Engin. classMethod (); document. write (n Ew Engin (). objectField + "<br/>"); document. write (Engin. classField + "<br/>"); // only class attributes and class methods can be traversed. // how can I traverse instance attributes and instance methods? Document. write ("use for in to traverse Engin objects ============================== <br/> "); for (var o in Engin) {document. write (o + "<br/>");} document. write ("============================================ ============================< br/> ");
// Define the Parent and define an attribute pField and a method pMethod var Parent = function () {this. pField = "pField"; this. pMethod = function () {document. write ("pMethod is called <br/>") ;}}; // defines the static attribute Parent of the Parent class. staticPField = "staticPField"; // defines the static Parent of the Parent class. staticPMethod = function () {document. write ("staticPMethod is called <br/>");} // defines the subclass Child, and defines an attribute cField and a method cMethod var Child = function () in the subclass () {this. cField = "cField"; // instance property this. cMethod = function () {// instance method document. write ("cMethod is called <br/>") ;}}; // defines the static attribute Child of the subclass. staticCField = "staticCField"; // defines the static Child method of the subclass. staticCMethod = function () {document. write ("staticCMethod is called <br/>");} // specify that Child inherits from Parent Child. prototype = new Parent (); // create a subclass object var childObj = new Child (); document. write (childObj. pField + "<br/>"); // The subclass object accesses the attributes of the parent class instance. // document. write (childObj. staticPField + "<br/>"); // The subclass object cannot access the static attribute childObj of the parent class. pMethod (); // subclass object calls the parent class instance method // childObj. staticPMethod (); // a subclass object cannot call the static document method of the parent class. write (childObj. cField + "<br/>"); // The subclass object accesses its instance attribute document. write (Child. staticCField + "<br/>"); // you must use the class name to access your own static attribute childObj. cMethod (); // The subclass object calls its own instance method Child. staticCMethod (); // you must use a class name to call your own static method.