JS object-oriented characteristics and value types and composite types

Source: Internet
Author: User

JS Object-oriented features already value types and composite types some properties
    1. Empty Object
      • An empty object is also an object, just a variable name with a variable, no object property
 
   
  
  1. var o = {};
    1. 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
 
   
  
  1. var num = 123;
  2. var num2 = num;
Value type Assignment characteristics
    1. Copy all the data in a variable, assign a variable
    2. var num = 123Indicates that the number in the stored variable is 123
    3. Copy 123 data, 2 data in memory
    4. copy the copied data tonum2
    5. Features 2 copies of data in memory
Reference (composite) type assignment
 
   
  
  1. var o = { name : ‘张三‘};
  2. var obj = o;
    1. A reference type assignment is a copy (address) of the data stored in the variable o, and then assigns the data to obj
    2. Only 1 copies of data in memory
    3. 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)
    1. 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
    2. 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
    1. 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 thedelete
    2. Access forms for object properties

      • Point syntax:o.name
      • Associative arrays:o[name]
    3. Mixing method (Extend)

  
 
  1. function extend(o1, o2){
  2. for(var key in o2){
  3. o1[key] = o2[key];
  4. }
  5. }
    1. If you need to add members dynamically to an object, you must use associative array syntax
  
 
  1. var o = {
  2. name : ‘张三‘,
  3. sayHello : function(){
  4. console.log( ‘Hello, 我叫‘ + this.name );
  5. }
  6. }
  7. console.log( o.name ); // 访问的是 o 的name 属性
  8. console.log( o[‘name‘] );
  9. o.sayHello();
  10. o[‘sayHello‘]; // []中的是字符串
Parameter passing
    1. 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.
 
   
  
  1. function foo(num){}
  2. var a = 123;
  3. foo(a);
* 函数在调用的时候, 首先需要将参数中的数据复制一份, 数字 123 复制一份* 跳转到函数中, 完成形参赋值, num = 123;* 进入函数体, 执行每一句函数
    1. 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
    2. 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
    1. The role of new, initializing the data
    2. 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
    1. Code:var 五类= new Person();
    2. 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
    3. 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
    4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.