Home  >  Article  >  Web Front-end  >  First introduction to the concept of objects in JS

First introduction to the concept of objects in JS

一个新手
一个新手Original
2017-09-26 09:50:031201browse

Object

1. First introduction to object

JavaScript An object is an unordered collection of keys and values, for example:

var person
 = {
    name: 'zhangsan',
    age: 20,
    tags: ['js','web','mobile'],
    city: 'Beijing',
hit:null
};

The keys of JavaScript objects are all string types, and the values ​​can be any data. type.

# Its properties can contain basic values, objects or functions. An object is actually a set of values ​​without order. We can imagine objects in JS as key-value pairs, where the values ​​can be data and functions.

Object behavior and characteristics

Characteristics - Properties

Behavior - Method

2. If a variable belongs to the object, then the variable can be called It is an attribute of the object. Attributes are generally nouns used to describe the characteristics of things;

#If a function belongs to the object, then the function can be called It is a method of the object. Method is a verb, describing the behavior and function of things

3.new keyword:

The constructor is a special function that is mainly used to initialize the object when creating it, that is, to assign initial values ​​to the object member variables. It is always used together with the new operator in the statement that creates the object. middle.

#1) The constructor is used to create a type of object, the first letter should be capitalized;

2) The constructor only makes sense when used with new.

new will do four things when executed

a.new will do Create a new empty object in memory

b. Let this point to this new object

c. Purpose of executing the constructor: in order to add attributes and methods to new properties of this object

d.new will return this object

Custom constructor

function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
this.sayHi = function(){
console.log('Hello,everyBody');
}
}
var p1 = new Person('张三',22,'actor','Beijing');

new creates a new empty object p1 Zhang San’s space, Call the execution function constructor to add attributes and methods to the p1 object;

4.this关键词

js中this的指向问题,有时候会让人难以捉摸,函数内部的this有几个特点:

1)函数在定义的时候this是不确定的,只有调用的时候才能确定;

2)一般函数直接执行,内部this指向全局window;

3)函数作为一个对象的方法,被该对象所调用,那么this指向的是该对象;

4)构造函数中的this其实是一个隐式对象,类似一个初始化的模型,所有方法和属性都挂载到了这个隐式对象身上,后续通过new关键字来调用,从而实现实例化。

遍历对象:

for(var key in obj){
console.log(obj[key]);
}

打印出来第一个obj[i]是

(对象的下标)

打印出来的第一个obj是遍历过i的对象

for(var key in obj){
console.log(key+"=="+obj[key])

}印出的结果是:

key是下标0,1,2,3,4,5,6...

obj[key]是遍历对应的下标对象的值

如果打印的是obj.key,因为.key是找对象的属性,该对象没有key属性所以是undefined;

取消对象属性,如果给对象定义key属性并赋值,想要取消key属性

delete obj.key;

The above is the detailed content of First introduction to the concept of objects in JS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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