First, the basic operation of object 1. Creation of objects
In JavaScript, you create objects in two ways: constructors and object literals .
(1) Constructors
var person = new Object ();p erson.name = "Tom";p erson.age = 22;person.say = function () {alert ("Hello");}
(2) object literal
var person = {Name: "Tom", age:22}
2. Deletion of object properties and methods
You can use the delete operator.
var person = {};p erson.name = "Tom";p erson.age = 20;person.say = function () {alert ("Hello!!!");} The delete operator deletes the properties and methods of the object delete Person.name;delete Person.say;alert (person.age); 20alert (Person.name); Undefinedalert (Person.say); Undefined
3. Traversal of objects
You can use the for in enumeration to iterate.
var person = {};p erson.name = "Tom";p erson.age = 20;person.say = function () {alert ("Hello!!!");} Traverse the object for (var attr in person) {alert (attr + "=" + Person[attr]);}
Ii. properties and methods of object 1.constructor properties
The constructor property is the constructor that holds the current object, and in the previous example, constructor saves the object method.
var person = {};p erson.name = "Tom";p erson.age = 20;person.say = function () {alert ("Hello!!!");} /* * Constructor: Save object Creation Function * Functions object () { *[native code] *} */alert (Person.constructor)
JavaScript Basic Learning (iv)-object