Home > Web Front-end > JS Tutorial > Introduction to the basics of JavaScript_object (must read) (graphic tutorial)

Introduction to the basics of JavaScript_object (must read) (graphic tutorial)

亚连
Release: 2018-05-21 09:52:15
Original
1391 people have browsed it

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 = {}; //实例化对象
Copy after login

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");
}
Copy after login

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';
Copy after login
获取对象的属性或者方法:对象.属性名/方法
alert(obj.name); // 张三
alert(obj.age); // 20
obj.say(); // hello World
Copy after login
delete 操作符 删除对象的属性或方法的
delete obj.age;
delete obj.say;
alert(obj.age);  //undified
alert(obj.sex);  //20
obj.say();   //报错,函数已被删除
Copy after login

Traverse a js object, for in statement

for(var attr in obj){
alert(attr + ":" + obj[attr]); //会按顺序将数组中的键值对打印,主要值,如果用对象.属性得到undified
}
Copy after login

Constructor saves the creation function of the object

alert(obj.constructor);
var o = [];
alert(o.constructor);
Copy after login
hasOwnProperty(propertyName) 用于检测给定属性在对象中是否存在,返回boolean类型,在项目中有时会用到,需留意
var i = {};
i.sex = '男';
alert(i.hasOwnProperty('sex')); //true
alert(i.hasOwnProperty('age')); //false
Copy after login
propertyIsEnumerable(propertyName)检测给定的属性是否能被for in 所枚举出来,返回boolean 
alert(i.propertyIsEnumerable('age')); //false 上面没有定义此属性
Copy after login

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)

Multi-threaded running in JavaScript Detailed analysis and answers to the library Nexus.js (part of the code attached)

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template