Create a simple instance of class Person and a class person instance

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.