Create a simple instance of class Person and a class person instance
Create a class Person with the following attributes: name, age, friend, and sayhi. For example, "Hello !") , Friend (addFriend method, add a value to friends), and then create two such instances: "Xiao Zhang", 22, ["Xiao Li", "Xiao Wang"], "Hello! "," James "," James ", 24, [" Ann "," Billy "]," Hello !"," Xiao Zhang"
Function Person (props) {this. name = props. name | 'anonymous'; this. age = props. age | 20; this. friends = props. friends | 'none';} Person. prototype. sayhi = function () {console. log ("Hello! "+ This. name);} Person. prototype. addFriend = function (newf) {this. friends. push (newf); console. log (this. friends);} function createPerson (props) {return new Person (props | | {});}
Var xiaozhang = createPerson ({name: 'zhang ', age: 22, friends: ["Xiao Li", "Xiao Wang"]}); xiaozhang. sayhi (); // hello! Xiao Zhang xiaozhang. addFriend ('James '); // ["Xiao Li", "Xiao Wang", "James"]
The simple example of creating a Person-like class in the above section is all the content shared by the editor. I hope you can give us a reference and support the house of helping customers.