This is the 23rd day of my participation in the August More Text Challenge.More challenges in August

preface

Hello, everyone. Today we are going to talk about the concept of object properties and stack in JavaScript. It is very easy to confuse the concept for non-students, so we are going to talk about this problem.

The article goal

Take a look at the goal of this article, please detour, because this article is basically excerpted from my notes when I started out

Focus on

1. Obtain and set the value of object properties. 2Copy the code

To understand

1. Know that memory is divided into stack and heap areasCopy the code

Action object

Operation object is relatively simple, skilled in the basic operation object method.

Gets the value of an object property

Note: undefined is returned if the object does not have an attribute.

Var obj = {name: 'obj ', age: 20} alert(obj) //' obj 'alert(obj) // undefinedCopy the code

Add attributes and assign values to objects

Var obj = {name: 'ls', age: 28, sex:' male '} var obj = {name: 'ls', age: 28, sex:' male '}Copy the code

Manipulate attributes of an object by enclosing parentheses syntax

Syntax: object [‘ attribute name ‘]

Var stu01 = {name:' this.name ', age:28, sex:true, getInfo:function(){return 'this.name ', age:28, sex:true, getInfo:function(){return' this.name ', age:28, sex:true, getInfo:function(){return 'this.name ', age:28, sex:true, getInfo:function(); ' + this.sex } }Copy the code

The operations are as follows:

Alert (stu01['name']) // alert(stu01['name']) // My name is stu01, age 28, gender true Alert (stu01['getInfo']()) // My name is small white, age 28, gender trueCopy the code

Assignment. Writing code in the following two ways works the same:

Name = 'stu01' STU01 ['name'] = 'stu01'Copy the code

Iterate over all properties in the object

Through the for… An in loop iterates through the properties of an object

Syntax: for(var variable name in the object being traversed){code for loop execution}

var obj = { name: 'zs', age: 18, sayHi:function(){ console.log('hi'); }};Copy the code

Object has several properties, and the for loop executes several times

During the loop, attributes in the object are assigned to the key variable in turn

Use bracket syntax in loops

for(var key in obj) {
  console.log(key + "==" + obj[key]);
}
Copy the code

Deletes an attribute from an object

var obj = {
     name: 'zs',
    age: 18
}
console.log(obj.age)    // 18
delete obj.age
console.log(obj.age)    // undefined
console.log(obj)    // {name: 'zs'}
Copy the code

Stack space and heap space in memory

All code in JS is loaded into memory for execution, and memory is actually divided into regions, for example, there are stack and heap areas in memory

Js 6 data types, and divided into two categories: basic data type and reference data type

The stack is equivalent to the address, and the heap is equivalent to the actual storage

  • Basic data types are stored in stack space
  • Reference data types are stored in heap space

The diagram is as follows:

Base data type and reference data type pass parameters

Base data type assignment

var num1 = 1; var num2; num2 = num1; // clone num1 and assign it to num2 console.log(num2); / / 1Copy the code

The diagram is as follows:

Reference data type assignment

var obj = {
    name: 'zs'
}
var num2
num2 = obj
console.log(num2) // { name: 'zs'}
Copy the code

The diagram is as follows:

Afterword.

Hello, I am the South Pole ice cube, a technology and appearance level proportional to the front-end engineer, advocating to solve front-end problems, I hope my blog has helped you.

Pay attention to me and walk together on the front road. Hey ~ 😛