Now I will bring you a basic introduction to JavaScript_object. Let me share it with you now and give it as a reference for everyone.
The base class of all Object classes
var obj = new Object(); var obj = {}; //实例化对象
There are two types of setting properties for objects:
1 .Use direct quantities: object.properties/methods, this method is intuitive and easy to understand
obj.name = '张三'; obj.age = 20; obj.sex = '男'; obj.say = function(){ alert("hello World"); }
2.Use "[]": object.['properties/methods '], when using this method, "" or '' must be added within the brackets, the method is strict
obj['birthday'] = '1989-08-07';
获取对象的属性或者方法:对象.属性名/方法 alert(obj.name); // 张三 alert(obj.age); // 20 obj.say(); // hello World
delete 操作符 删除对象的属性或方法的 delete obj.age; delete obj.say; alert(obj.age); //undified alert(obj.sex); //20 obj.say(); //报错,函数已被删除
Traverse a js object, for in statement
for(var attr in obj){ alert(attr + ":" + obj[attr]); //会按顺序将数组中的键值对打印,主要值,如果用对象.属性得到undified }
Constructor saves the creation function of the object
alert(obj.constructor); var o = []; alert(o.constructor);
hasOwnProperty(propertyName) 用于检测给定属性在对象中是否存在,返回boolean类型,在项目中有时会用到,需留意 var i = {}; i.sex = '男'; alert(i.hasOwnProperty('sex')); //true alert(i.hasOwnProperty('age')); //false
propertyIsEnumerable(propertyName)检测给定的属性是否能被for in 所枚举出来,返回boolean alert(i.propertyIsEnumerable('age')); //false 上面没有定义此属性
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Detailed analysis of javascript callback functions (graphic tutorial)
Detailed analysis and answers to the principle of JavaScript operation
The above is the detailed content of Introduction to the basics of JavaScript_object (must read) (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!