Create a class Person, which contains the following properties: name (name), age (age), friends (friends array), greeting (sayhi method, output greetings, such as: "Hello!"), make friends (addFriend method, add a value to friends), and then create two instances of this class: "Xiao Zhang",22,["Xiao Li","Xiao Wang"],"Hello!","James","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:'Xiaozhang',age:22,friends:["Xiao Li","Xiao Wang"]});xiaozhang.sayhi();//Hello! Xiao Zhang xiaozhang.addFriend('James');//["Xiao Li", "Xiao Wang", "James"]The above article is a simple example of creating a Person-like content that the editor shares with you. I hope you can give you a reference and I hope you can support Wulin.com more.