First, the class in JavaScript

Source: Internet
Author: User

1. Find the object's constructor----constructor/instanceof

constructor is an extra property that comes with the template instantiation of the object, which points to the JavaScript constructor that was used when the object was created.

Usage:

var New  = = = Accommodation;    // true

This comparison can also be done with the keyword instanceof,

var New  instanceof accomodation;   //

2. Adding properties and methods through prototypes

Each function in JavaScript, that is, each constructor, has a property called prototype. When we use the keyword new to create an object instance of "Class", the properties and methods contained in the instance are from prototype.

You can add custom properties and methods to all instances of a template with dot notation and Direct volume notation.

Point marking Method:

// define a constructor named accommodation function accommodation () {} // Add a property to this "class" Accommodation.prototype.floor = 0= 0=false;

Direct Volume Labeling Method:

// define a constructor named accommodation function accommodation () {} // add properties and methods to this "class" by the direct amount of the object Accommodation.prototype = {floors:0, Rooms:0, sharedentrance:false, Lock:  function() {},}

Prototype This property keyword has a powerful feature that allows you to continue adding properties and methods after the instance of the play has been created, and these newly added properties and methods are automatically added to all object instances, whether created or created.

3. Adding properties and methods through scopes

If a variable or function is defined outside of any function and placed directly in a JavaScript or HTML file, then the variable or function is added to the global scope, which means that the variable or function can be used anywhere in the code, even within other function bodies.

4. Context and this keyword

Benefits of using this: you can arbitrarily change the variable name of an object without worrying about the behavior of the method in the object being affected.

Context: In most cases, the object that encapsulates the function when the function is run, and when it is not called separately by any object, the context is worth the global Window object.

Note: The escape function in an object whose context is the global Window object, not the object that contains it, can store the value of this in a variable when this is pointed to the object containing the function, and then use that variable instead when the object is used. For example, use the variable name: that

When you create an object using the New keyword, the value of this is different from the general case. This refers to the object instance created by the constructor. It is because of this feature that we proudly set the properties and methods of all object instances through this in the constructor, rather than using the prototype keyword as before.

Combines the prototype and this keyword to define properties and methods for an object instance, where the former defines the method, which defines the property.

5. Chaining calls to methods

If you want to be able to make chained calls to a method, you need to return the reference to the object instance through the This keyword at the end of the method in the class.

First, the class in JavaScript

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.