JS Object-oriented features already value types and composite types some properties
- Empty Object
- An empty object is also an object, just a variable name with a variable, no object property
var o = {};
- Parameter passing
- Value type: Two variables inside and outside the function, two data are not the same
- Reference type: function inside and outside variables, one data, same address
Assign value
var num = 123;
var num2 = num;
Value type Assignment characteristics
- Copy all the data in a variable, assign a variable
var num = 123
Indicates that the number in the stored variable is 123
- Copy 123 data, 2 data in memory
- copy the copied data to
num2
- Features 2 copies of data in memory
Reference (composite) type assignment
var o = { name : ‘张三‘};
var obj = o;
- A reference type assignment is a copy (address) of the data stored in the variable o, and then assigns the data to obj
- Only 1 copies of data in memory
- Feature is two variables, one data, point to the same address
Deep copy and shallow copy (guarantees that the object's attributes are also reference types)
-
The concept of deep copy
- Copy, the value of all reference types in the data, all copies one copy, the data in memory independent, is deep copy
- Modify copy data and source data, no contact
- Features: Memory isolation, completely independent
The concept of shallow copy
- Copy, only the properties of the current object are copied, and the reference type does not consider
Dynamic properties of objects
In JS, an object needs a property, it can be used to 对象.属性名 = 值
add members to this object, as long as the assignment succeeds, the object adds new properties
- To add a method, you add a method
- To add an attribute, add a property
- To remove a property or method, use the
delete
Access forms for object properties
- Point syntax:
o.name
- Associative arrays:
o[name]
Mixing method (Extend)
function extend(o1, o2){
for(var key in o2){
o1[key] = o2[key];
}
}
- If you need to add members dynamically to an object, you must use associative array syntax
var o = {
name : ‘张三‘,
sayHello : function(){
console.log( ‘Hello, 我叫‘ + this.name );
}
}
console.log( o.name ); // 访问的是 o 的name 属性
console.log( o[‘name‘] );
o.sayHello();
o[‘sayHello‘]; // []中的是字符串
Parameter passing
- As a parameter in a function, it is to copy the data of the parameter, and pass it to the formal parameter of the function.
function foo(num){}
var a = 123;
foo(a);
* 函数在调用的时候, 首先需要将参数中的数据复制一份, 数字 123 复制一份* 跳转到函数中, 完成形参赋值, num = 123;* 进入函数体, 执行每一句函数
- A value type is a feature passed as a function parameter that is two different variables inside and outside the function, except that the values are equal
- A reference type is a feature passed as a function parameter that is two different variables outside the function, but points to the same self, pointing to the same object
- Inside the function allows modifying the data of an object outside the function
Function of constructor constructor function
- The role of new, initializing the data
- When JS adds a property to an object, it uses the dynamic properties of the object in conjunction with this (where this refers to an instance object created using the constructor) to assign a value, add a member
The process by which the constructor creates an object
- Code:
var 五类= new Person();
- Use new to create an object that is similar to
o = {}
an empty object without any members
- Use new to create an object whose type is to create his constructor name
- Use {} to create an object, which
new Object
is equivalent to using, his type is Object
- Call the constructor, create the instance object, initialize the member
- The constructor has an assignment operation at the beginning of the call, which points to the instance object (the reference address) that just created the error.
- Hidden in constructor this represents the object that was just created
- Adding a member to an instance object by using this in a constructor to combine the dynamic properties of an object
From for notes (Wiz)
JS object-oriented characteristics and value types and composite types