JavaScript objects are understood at first sight!

What is an object?

#Any entity that occupies memory is an object (everything in nature is an object)

面向对象方法的核心是: 封装,继承,多态。
 我们知道对象是以现实世界的对象为模型构造而来的,具有状态和行为,
 其中状态保存在一组状态中,而对象的行为通过对象方法来实现的。
 我们可以用软件对象来表示现实世界中对应的对象。
(In real life, a car is an object. We know that an object has attributes and methods. Car attributes: color, weight, brand car method (function): start, drive, stop, etc.)

In JavaScript, we can understand that objects are containers of variables.

  1. The writing of JavaScript object key-value pairs is a bit similar: associative arrays in PHP and hash maps in Java.

    var a = 1; // Declares a variable of numeric type
    var b = "";//Defines a variable of string type
    var c; //Declares a variable named c

     a和b都是对象,也就说任何实际占据内存的东西都可以看成对象。   
     JavaScript中 函数、数组、基本数类型变量 都属于对象
     而类(类一种格式,本身不是数据,只是一个模板) 不是对象,所以不会占据内存空间。
    

    Note:
    Declaring a variable only tells the compiler about the variable name identifier, so that the compiler "recognizes" the identifier, and does not necessarily cause memory allocation.
    Summary:
    1) Declaring a variable does not necessarily occupy memory space;
    2) When defining a variable, the system will allocate memory space for the variable to store the corresponding type of data. The variable name is the name of the corresponding memory unit.

1. Create an object:


  //方法一:字面量的方式创建一个对象;
     var obj = {
    
    };//定义了一个空对象,
     //可以向里面添加成员
     obj.name = "小李";
     obj.age=12;
     console.l
     var person = {
    
    
         firstName:"gogo",
         age:15,
     sex:"man"
     };
 //方法二:通过内置构造函数的方式创建对象
//Object是js内置给我们的构造函数,用于创建一个对象使用
        var obj = new Object();  
        obj.name = "Tom";
        obj.age=66;


2. Object method:

  /*  对象方法作为一个函数定义存储在对象属性中。
         案例一:修改并打出学生个人信息 */
    var student = {
    
    
           Name: "李四",
           sex : "男",
           id : 1701170,
           printInfo : function() 
           {
    
    
           return this.Name + " " + this.sex + "" + this.id;
           }
             }
           var  zhansan = student
                zhansan.Name="张三";//修改成员属性值
        
       document.write(zhansan.printInfo());   
     

2. Class (new syntax):

//汽车类(class 属于新语法存在兼容性问题)
 class Car{
    
    
     constructor(color,price){
    
    
         this.color = color;
         this.price = price;
     
     getColor(){
    
    
         return this.color;
     }
    }
  }
 var c = new Car("red",12);
 console.log(c.getColor());

3. Understand the constructor:

The first step in object-oriented programming is to generate objects. Object-oriented programming in js is based on the constructor and the prototype chain.

As mentioned earlier, the "object" is an abstraction of a single object. Usually a template is needed to represent the common characteristics of a certain type of physical object, and then the "object" is generated based on this template.
In the js language, a constructor is used as an object template. The so-called constructor is a function that provides a template for generating an object and describes the basic structure of the object.
A constructor can generate multiple objects, each of which has the same structure.

//示例1
function createPeople(name,sex) {
    
    
     var obj  = new Object();
     obj.name = name;
     obj.sex = sex;
     obj.setName = function(name) {
    
    
         this.name = name;
     }
     return obj;
 }
 
//实例化对象               
 var p = createPeople("tom")
 console.log(p.name);

//实例2
function People(name,sex) {
    
    
    this.name = name;
    this.sex = sex;
 function getName () {
    
    
        return this.name;
    }
}

People.prototype.getSex = function() {
    
    
    return this.sex;
}

//实例化对象
var p1 = new People("ls","a");
console.log(p1.getName())
console.log(p1.getSex())

Guess you like

Origin blog.csdn.net/qq_40961508/article/details/107431336