I was always a little confused when writing Java before, and most of them used jQuery, but the principle is not very clear. I have been learning JavaScript in the system recently. Please point out any questions or errors, thank you.........
Basic classes of all classes in Object
var obj = new Object();
var obj = {}; //Instantiate the object
There are two types of properties for the object:
1. Use direct quantity method: object. attribute/method, this method is intuitive and easy to understand
obj.name = 'Zhang San';
obj.age = 20;
obj.sex = 'male';
obj.say = function(){
alert("hello World");
}
2. Use "[]" method: object.['Properties/Methods']. When using this method, "" or '' must be added in brackets, and the method is relatively strict.
obj['birthday'] = '1989-08-07';
Get the attribute or method of the object: object. Attribute name/method
alert(obj.name); // Zhang San
alert(obj.age); // 20
obj.say(); // hello World
delete operator deletes the object's properties or methods
delete obj.age;
delete obj.say;
alert(obj.age); //undified
alert(obj.sex); //20
obj.say(); //An error is reported, the function has been deleted
Iterate through a js object, for in statement
for(var attr in obj){
alert(attr + ":" + obj[attr]); //The key value pairs in the array will be printed in order, the main value will be obtained if the object. attribute is used to obtain undified
}
Constructor saves the creation function of the object
alert(obj.constructor);
var o = [];
alert(o.constructor);
hasOwnProperty(propertyName) is used to detect whether a given property exists in the object, returns the boolean type, which is sometimes used in the project, so please pay attention to it
var i = {};
i.sex = 'male';
alert(i.hasOwnProperty('sex')); //true
alert(i.hasOwnProperty('age')); //false
propertyIsEnumerable(propertyName) detects whether the given property can be enumerated by for in and returns boolean
alert(i.propertyIsEnumerable('age')); //false This property is not defined above
The above article about JavaScript_object basics (must-read) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.