JavaScript some important points of knowledge combined with the performance of the topic!

Source: Internet
Author: User
Tags instance method

functionfoo () {//① declares a function of Foo getName=function() {alert (1);     }; return  This;} Foo.getname=function() {Alert (2);}; ② creates a static property called GetName for Foo to store an anonymous function Foo.prototype.getName=function() {Alert (3);}; ③ Create an anonymous function called GetName for the prototype object of FoovarGetName =function() {Alert (4);}; ④ creating a getname function from an expressionfunctionGetName () {alert (5);}  ⑤ declares a function called GetName ①foo.getname (); //2 Direct access to static properties stored on the Foo function 2②getname ();//4③foo (). GetName ();//1④getname ();//1⑤NewFoo.getname ();//2
⑥new Foo (). GetName ();
⑦new New Foo (). GetName ();

Popular Science static properties and static methods;

functionMyobject () { This. instanceprop= "456"; Instance Property Instanceprop This. instancemethod=function() {};//instance Method Instancemethod} Myobject.prototype.protoProp= "789"; Myobject.prototype.protoMethod=function(){}; Myobject.staticprop= "123"; The property added in this way is called static property Myobject.staticmethod=function() {//This method of adding Staticmethod is called static method};varmyobject=NewMyobject ();


The above properties and methods are all dependent on the object, JS is the object, the function is the object
After assigning a value to the prototype object of a function, it will exist.
An instance object created with this function as a constructor can access the properties and methods of the prototype

First ask the knowledge point:

    • Calling public methods, public properties, we must first instantiate the object, that is, using the new operator to instantiate the object, the method and properties of the object can be instantiated, and the public method cannot call the private method and the static method
    • Static methods and static properties are the calls that we can invoke without instantiating
    • While the private methods and properties of an object are not accessible externally

Second point of Knowledge:

    • There is a mechanism by which a variable declaration is promoted in the JavaScript interpreter, that is, the function declaration is promoted to the front of the scope, even if the code is written in the last face, it will be promoted to the front.
    • A function created with a function expression is assigned at run time and will not be called until the expression has been assigned to the completion value.

There is a difference between function declarations and function expressions in JavaScript , function declarations are promoted in JS parsing, so in the same scope, the function can be called regardless of where the function declaration is defined. The value of the function expression is determined at the JS runtime and is called after the expression assignment is complete.

The third question knowledge points:

  Foo().getName();The Foo function is executed first, and then the GetName property function of the return value object of the Foo function is called.

When executing the Foo function, because it is an assignment statement, (no var declaration ), we first look for the getname variable in the scope of the Foo function, and find out if there is a getname variable to the upper scope of the function. is actually a function of the declaration of the ④ expression, which is then assigned to function () {alert (1);}, after which the Foo function returns this, and the call points to himself. The answer at this time is 1;

Knowledge Points: This point, function expression declaration in advance, scope;

The four Points of knowledge: PS: Directly call the GetName function relative to Window.getname (); This variable has been modified by the Foo function, so the third question is the same;

The knowledge points of the question five:

The priority of access is higher than the new () parameterless operator reference operator Precedence https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/ Operator_precedence

The six and seventh questions are the methods that instantiate the object and then invoke the instantiated object, so get 3

Original link: https://juejin.im/entry/58db95eaac502e0058f8472e?from=timeline

JavaScript some important points of knowledge combined with the performance of the topic!

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.