ES5- Object oriented

One: stack and heap memory space

//---------- Value type (stack)----------
var a = 10;
var b = a;
a++;
console.log(a)/ / 11
console.log(b)/ / 10
//---------- Reference type (heap)----------
var aa = {};
var bb = aa;
aa.name = 'Change together';
console.log(aa);//{name: change together}
console.log(bb);//{name: change together}
Copy the code

Two: value equal == and congruent ===

// Define object and string comparisons
var a1 = new String('123'); / / object
var a2 = String('123');//string
var a3 = '123'; //string
a1 == a2   //true
a1 ===a2  //false
a2==a3    a2===a3  //true
var b1 = new String('123'); / / object
var b2 = new String('123'); / / object
console.log(b1 == b2) //false different address
JSON.stringify(b1) == JSON.stringify(b2) //true
Copy the code

Three: value pass check reference pass

// On value passing (changes inside a function do not affect the outside) is equivalent to assigning to another variable
var num = 50;
function add(n) {
    n = 100;
}
add(num);
console.log(num);
/ / reference | shortcut (pointer) passed internal changes will affect the external (function)
var arr = [10.20.30];
function add2(arr) {
    console.log(arguments)
    arr[0] = 99;
}
add2(arr);
console.log(arr);
Copy the code

Four: object definition

//----------object type constructor ----------
var obj1 = new Object(a); obj1 .name ="Xiao Ming";
obj1.sleep = function () {
    console.log(this.name + "Sleeping");
};
//---------- literal definitions (simple literals) ----------
var obj2 = {};
obj2.name = 'alney';
obj2.age = '10';
var mnk = 'name2'; // 'name2 ' , name3
obj2[mnk] = 'Alice';
//---------- literal definition (nested literals) ----------
var per01 = {
    name: 'laney'.age: '10'.action: function () {
        console.log('run')}}//---------- factory way ----------
function createPerson(name, age) {
    var obj = new Object()
    obj.name = name;
    obj.age = age;
    obj.action = function () {
        console.log('run')}return obj;
}
var c1 = createPerson('hong'.20)
var c2 = createPerson('ming'.30)
//---------- constructor ----------
function Person(name, age) {
    this.name = name;
    this.age = age;
    this.action = function () {
        console.log('run' + this.name)
    }
}
var p1 = new Person('laney'.20)
Copy the code

Five: prototype chain

var pm = {
    name: 'alice'
};
var newpm = Object.create(pm)
newpm.__proto__ === pm// It is the person who creates the object prototype chain
Copy the code