JavaScript's constructor

Source: Internet
Author: User

JS in the constructor function:

//constructor FunctionfunctionDog (Sex) {//Public Properties     This. Sex =sex;  This. Name = ' Blog '; //Private Properties     varSound = ' Wang Woo '; varthat = This; //Private Methods    functionShowName () {console.log (that.name); } showname ();} /** Add public properties/public methods to constructors * The prototype * Prototype property is required: gives you the ability to add properties and methods to objects. * Adding a member to prototype will add the new method to the bottom of the constructor*///Public PropertiesDog.prototype.color = ' Red ';//Public MethodsDog.prototype.run =function() {    return"Ten km/h";} /** Add static Properties/static methods to constructors * Note some special properties: If name defaults to the method name, modifying this static name may always be the method name*///Static Propertiesdog.address = ' Mars ';//Static MethodsDog.showsex =function(dog) {returnDog.sex;}//instantiation ofvarDog1 =NewDog (' Boy '));//1. Calling public properties/methodsConsole.log ("Color:", Dog1.color)//RedConsole.log ("Run:", Dog1.run ())//ten km/h//2. Calling private properties/methodsConsole.log ("Address:", dog1.constructor.address)//MarsConsole.log ("Showsex:", Dog1.constructor.showSex (DOG1))//Boy 

3. Attribute Method view
Console.log ("constructor:", Dog1.prototype)//UndefindConsole.log ("prototype-dog-:", Dog.prototype)//{Object}Console.log ("constructor:", Dog1.constructor)//function () {}Console.log ("constructor-dog-:", Dog.constructor)//function () {}

JavaScript's constructor

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.