JavaScript Objects (Day 4)

In object-oriented programming, JavaScript does not fully have encapsulation, inheritance, and polymorphism; in JavaScript, an object is an unordered collection of key-value pairs

Encapsulating JavaScript is available, defining properties and methods inside the object;

Inheritance can be implemented in the form of dynamic properties in javascript; that is, traversing the properties and methods of one object, and then dynamically adding it to another object;

Polymorphism has no concrete manifestation in JavaScript;

There are several ways to create objects in JavaScript:

The first way, literal (may be the more commonly used one)

var obj={

  name:"Zhang Fei",

  age:34

};

The second. Built-in constructor method

var obj=new Object();

obj.name="Zhang Fei";

obj.age=34;

Third, the custom constructor method

function Person(){

  this.name="张飞";

  this.age=34;

  this.speak=function(){

    console.log("speaking");

  }

  return;//If the basic data type is returned, it will not affect the effect of using new Person() to create an object, so there is no need to return here; but if the object type is returned, it will affect; for example return {};

}

var p = new Person ();

p.name;

p.speak();

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325383980&siteId=291194637
Recommended