JavaScript OOP object-oriented programming

Source: Internet
Author: User

Pseudo-class Declaration

Original address: Http://javascript.info/tutorial/pseudo-classical-pattern#pseudo-class-declaration

A Pseudo-class consists of the constructor function and methods.
For example, here's the Pseudo-class with single method and both Animal sit properties.

function Animal (name) {  THIS.name = Name}animal.prototype = {   canwalk:true,  sit:function () {    This.canwalk = False    alert (THIS.name + ' sits down. ')  }} var animal = new Animal (' Pet ')//(1) alert (animal.canwalk)//Trueanimal.sit ()             //(2) alert (Animal.canwalk)//False

  

    1. When new Animal(name) was called, the new object recieves __proto__ reference to, see this on Animal.prototype the "the" picture.
    2. Method animal.sit Changes animal.canWalk in the instance, so now this animal object can ' t walk. But the other animals still can.

The scheme for a pseudo-class:

    • Methods and Default properties is in prototype.
    • Methods prototype in use this , which are the current object because the value of only depend on the this calling Co ntext, so animal.sit() would set this to animal .

JavaScript OOP object-oriented programming

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.