Js--class Foundation

Source: Internet
Author: User

JS class? Actually, it's a prototype!

1 class point{2    Constructor (x, y) {3this         . x = x; 4         this. y = y; 5     }6    toString () {7         return this this. Y + ') '; 8     }9 }

Basic points

1. Class is the keyword, and class point defines a category. Other variable names cannot be the same as the class name

2. Type of point (typeof): function, must be called by new

    • Class pototype=> contains all defined methods (including constructor) in class
      Point.protoype; // {constructor:?, toString:?}

3. Like a function, a class can also be defined in the form of an expression

    • var MyPoint = class{}; var New MyPoint ();

      No variable promotion: You must define before you can use class

    • The Name property (like a function name), similarly, class names can only be used inside the class
      Mypoint.name; // MyPoint var MyPoint2 = class me{}; Mypoint2.name; // MyPoint2 new Me (); // error, me undefined, can only be used inside the class

4. Constructor is a constructor

    • An object is instantiated with new point, and the Contructor in class is automatically called
    • Return value: Default Return instance object this, can specify other return value (same as ES5)
    • Can be omitted? If you do not explicitly write a constructor, an empty (same as C + +) is added automatically
    • Point.prototype.contructor is class, not contructor function
    • var New Point (); Console.log (point.constructor===point); // true

      The this in constructor points to the instantiated object, as in the ES5

      Console.log (point); // Point {x:undefined, y:undefined}

5. Definition of Object properties: Use this in constructor to add; Definition of object methods: defined directly in class (without function and comma)

6. Methods defined in class are not enumerable

1 object.keys (point.prototype); // NULL result, description not enumerable 2 // Enumerable in Es5 3 function Test () {} 4 function (){}; 5 Object.keys (Test.prototype); // [FUN1] Enumerable
View Code

7. Instance Objects

    • Share a prototype object (same as ES5)
    • var New Point (); var New  = = = p2.__proto__; // true

      The method of the prototype object can be added directly to the class, if you want to add a property =>getprototypeof/point.prototype

      point.__proto__ = = = Point.prototype; // true var pproto == 1;p oint.newattr; // 1

8. Static methods and properties (Static)

    • They belong to class, not attribute instances (same as C + +)
    • Static properties can only be added by classes outside of class
    • class staticclass{static    constructor () {// Define a static method        Console.log (this) ; // class{...}     }    Constructor () {        console.log (this);   point to instance object      = 1;   Define a static property

      This in the static method points to the class!

9. New properties of New.target

  • The function or class is called with new, then returns itself using new.target inside them, otherwise returning undefined
  • can be used to distinguish whether a function is called by new
  • function Point1 () {    console.log (new. target);} Point1 (); // undefined var New Point1 (); // function Point1 () {...}

    Use in class

    var targetclass = class me{    constructor () {        console.log (newTarget===targetclass ); // true        Console.log (newtarget===me); // true     }}varnew targetclass ();

Js--class Foundation

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.