Similar to Delphi/C #, the ancestor of all objects in Javascript is object. Although not all objects are object-oriented, it seems to be object-based.
For example, a string variable can use the attributes and methods of the string object, which is converted to an object at the moment of the call.
Two methods for creating original empty objects
VaR obj1, obj2; obj1 = new object (); obj2 = {}; alert (obj1); // [object]-objectalert (obj2) of the object class ); // [object] // The following six methods can be used for the objects created using the above two methods (rather than abstract methods): hasownpropertyisprototypeofpropertyisenumerabletolocalestringtostringvalueof // does the object look like an interface? Like this, because the above method does not force sub-classes to implement. // now it should immediately remind people of the two empty array creation methods.CubeMethod: var arr1, arr2; arr1 = new array (); arr2 = []; // array as the subclass of the object, inherits or re-implements the above method, in addition, more array-specific methods such as sort are added. // One is {} and the other is [], which is very interesting. More interesting, they can contain each other.
Define your own objects
// An array, number, date, Regexp, and other practical classes (or object templates) already exist. What if we build our own classes? // Simple, but hard to understand: function rectangle (W, h) {This. W = W; this. H = H;}/* instantiate a */var myobj = new rectangle (11, 22); alert (myobj. w); // 11 alert (myobj. h); // 22 alert (myobj); // [object] // You Can See That myobj is an object. In JS, functions are also objects, and a function class exists. // can be found in rectangle (function? Class ?) To add more methods or attributes.
JavaScript Object Notation-JSON
// JavaScript Object Notation (JavaScript Object Notation) is abbreviated as JSON. // when it comes to JSON, admire its designers, it is a rule used to describe objects in a language and is now regarded as a data format that surpasses XML. vaR OBJ = {Name: 'zhangsan', age: 88, marriage: true}; // The data in {} is separated by commas (,). Each data is in the form of name: value; //: The identifier is indispensable. It will be the property or method of the object. It can also be enclosed in quotation marks or Chinese characters. /* Access value method 1 */Alert (obj. name); // Zhang San alert (obj. age); // 88 alert (obj. marriage); // true/* method 2 of access value */Alert (OBJ ['name']); // Zhang sanalert (OBJ ['age']); // 88 alert (OBJ ['marriage']); // true/* traversing value */For (var k in OBJ) {alert (OBJ [k]); // Zhang San/88/true}/* traverse name */For (var k in OBJ) {alert (k ); // Name/age/marriage} // the object can continue to contain objects and nested with arrays. Therefore, JSON can express a very complex data structure.
Add methods to custom classes
VaR OBJ = {Name: 'zhang san', age: 88, marriage: false, Info: function () {return this. name + 'Year' + this. age + 'years old, '+ (this. marriage? 'You are married. ':' unmarried. ') }}; alert (obj.info (); // Zhang San, 88 years old. He is not married yet. alert (OBJ ['info'] (); // Zhang San is 88 years old and is not married yet. OBJ. name = 'Li si'; obj. age = 18; obj. marriage = true; alert (obj.info (); // Li Si is 18 years old and married. // There are other methods.