[JS Notes: JS JavaScript objects related to the object]

Object overview:

Overview: Object belonging to one type of reference data, a plurality of different data types can be stored in an object attribute, used to hold its property value, the attribute value can be any data type.

Object classification:

  1. Built-in objects
    • ES objects defined by the standards, can be used in the implementation of any ES
    • For example: Math Object, String objects, Number objects, Boolean objects, Function objects, Object Object ...
  2. Host object
    • JS by an object operating environment provided by the current situation mainly refers to the object provided by the browser
    • For example: BOM objects, DOM objects
  3. Custom object
    • By the developers to create their own objects

Create an object:

  • Use the new keyword function call, the constructor constructor, the constructor function is designed to create an object, use typeof examination of an object, returns object; you can create a function uses the form of an object literal.
//创建一个普通对象
var obj = new Object();
var obj = {};
//创建一个函数对象
var fun = new Function();
function fn(){};
//创建一个数组对象
var arr = new Array();
var arr = [];
//创建一个正则对象
var reg = new RegExp(pattern, attributes);
var reg = //;
//创建一个字符串对象
var str = new String(s);
var str = "s";
//创建一个布尔对象
var bool = new Boolean(value);
var bool = true;
//创建一个数值对象
var myNum = new Number(value);
var num = 1;
//创建一个日期对象
var myDate = new Date();

Value added or modified attributes to the subject

Syntax: Object attribute name = attribute value;

//创建一个obj对象
var obj = new Object();
//向obj中添加一个name属性,其属性值为“孙悟空”
obj.name = "孙悟空";
//向obj中添加一个gender属性,其属性值为“男”
obj.gender = "男";
//向obj中添加一个age属性,其属性值为18
obj.age = 18;
//修改name属性的属性值为“猪八戒”
obj.name = "猪八戒";

Reading object attributes

Syntax:
The first way: Object attribute name.
The second way: Object [ "attribute name"] = attribute value

** Note: ** If you use a special attribute name, not by way of the first embodiment to operate, such as the class name attribute is a variable, you should use the second approach. If the object does not attribute to read, does not complain but will return undefined.

var obj = new Object();
//向obj中添加一个name属性,其属性值为“孙悟空”
obj.name = "孙悟空";
//向obj中添加一个gender属性,其属性值为“男”
obj.gender = "男";
//向obj中添加一个age属性,其属性值为18
obj.age = 18;
//读取obj对象中的gender属性值
console.log(obj.gender); // 输出“男”
console.log(obj.hello); //因为obj对象不存在hello属性,因此输出undefined

Property deleting objects

Syntax:. Delete object property name

var obj = new Object();
//向obj中添加一个name属性,其属性值为“孙悟空”
obj.name = "孙悟空";
//向obj中添加一个gender属性,其属性值为“男”
obj.gender = "男";
//向obj中添加一个age属性,其属性值为18
obj.age = 18;
//删除obj对象中的name属性
delete obj.name;

Determine whether an object has a property

Syntax: "property name" in the object
of the statement will have a return value, if it returns true, not false returns

var obj = new Object();
//向obj中添加一个name属性,其属性值为“孙悟空”
obj.name = "孙悟空";
//向obj中添加一个gender属性,其属性值为“男”
obj.gender = "男";
//向obj中添加一个age属性,其属性值为18
obj.age = 18;
//判断obj对象中是否含有“name”属性
console.log("name" in obj); // 输出“true”
//判断obj对象中是否含有“hello”属性
console.log("hello" in obj); //输出“false”
Published 20 original articles · won praise 11 · views 1747

Guess you like

Origin blog.csdn.net/qq_16221009/article/details/103098353