create object

Several ways to create objects

The first: literal

The second: through the constructor

The third type: Object.create

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CreatObject</title>
</head>
<body>

</ body > 
< script > 
    // The first way 
    var o1 = {name: " o1 " };
     var o2 = new Object({name: " o2 " });
    // The second way 
    var A = function ( name) {
         this .name = name;
    };
    var o3 = new A( " o3 " );
     // The third way 
    var c = {name: " c " };
     var o4 = Object.create(c);
    console.log("o1",o1);
    console.log("o2",o2);
    console.log("A",A);
    console.log("o3",o3);
    console.log("o4",o4);
</script>
</html>

ky can see that the structure of the objects created by the first two methods is the same, and the structure of the object created by the third method Object.create is different from the others. He first creates a {}, and then points the __proto__ of this object to the Input parameters, note that the reference is passed in, if we change o4.name then c.name also changes

Guess you like

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