JavaScript Essentials 1

Source: Internet
Author: User

1.IIFE (Execute function Now)

  Execute the function immediately, that is, immediately invoked function Expression (iife), just as its name is when the function is created and executed immediately . It does not bind any events, and it does not have to wait for any asynchronous operations:

(function () { //Code})(); function () {...}is an anonymous function that encloses its pair of parentheses and converts it to an expression, followed by a pair of parentheses that calls the function. Execute function immediatelyIt can also be understood that an anonymous function is called immediately. Execute function immediatelyThe most common scenario is to limit the scope of the var variable to your functions so that you can avoid naming conflicts. 2.prototypeEach JavaScript constructor has a prototypeProperty that sets the properties and methods that all instance objects need to share. prototypeproperty cannot be enumerated. JavaScript only supports the use of prototypeproperty to inherit properties and methods.function Rectangle (x, y) {this._length = x;this._breadth = y;} Rectangle.prototype.getDimensions =function () {return {Length: this._length, breadth: this._breadth }; };var x = New Rectangle (3, 4); var y = new Rectangle (4, 3); Console.log (X.getdimensions ()); //{length:3, breadth:4} Console.log (Y.getdimensions ()); //{length:4, breadth:3} in the code, x and y are the object instances created by the constructor Rectangle , which inherit the Getdimensions method through prototype. 3. ModularJavaScript is not a modular programming language, at least ES6 before landing. For a complex web application, however, modular programming is a fundamental requirement. At this point, you can use the immediate execution function to achieve modularity, just as many JS libraries such as jquery and our fundebug are implemented. var module = (function () { var N = 5; function Print (x) { Console.log ("The result is:" + x); }function Add (a) { var x = a + N;print (x); }return {Description: "This is description", Add:add}; })();Console.log (module.description); //Output "This is description" Module.add (5); //output "The result is:10" modularity is the control of the accessibility of properties and methods within the module, either private or public, as needed. In code, module is a stand-alone block, N is its private property, print is its private method, decription is its public property, and add is its common method. 4. Variable PromotionJavaScript moves all variables and function declarations to the front of its scope, which is called variable elevation (hoisting). In other words, wherever you declare variables and functions, the interpreter moves them to the front of the scope. So we can use variables and functions first and then declare them. However, only variable declarations are promoted, and variable assignments are not promoted. If you don't understand this, sometimes it goes wrong:  Console.log (y); //output undefined y =2; //Initialize y The above code is equivalent to the following code:var y; //Declaration y Console.log (y); //output undefined y =p; //Initialize y to avoid bugs, developers should declare variables and functions at the beginning of each scope. 5. currying   Curry, or currying, can be a function that becomes more flexible. We can call it by passing in more than one parameter at a time, or you can just pass in a subset of the arguments to call it, and let it return a function to handle the remaining arguments.var add = function (x) { return function (y) { return x + y;}; };Console.log (Add (1) (1)); //Output 2 var add1 = Add (1); Console.log (Add1 (1)); //Output 2 var = add (Add10); Console.log (ADD10 (1)); //Output Onecode, we can pass 2 1 as the parameter Add (1) (1)at a time, or we can get the add1 and add10 functions after passing in 1 parameters, which is very flexible to use. 6.apply, call and bind methods   JavaScript developers need to understand ApplyPagerAnd BindDifferent points of the method. What they have in common is that the first parameter is This, which is the context on which the function is run. Among the three, PagerMethod is the simplest, which is equivalent to specifying ThisValue Call Function:var user = {Name: "Rahul mhatre", Whatisyourname: function () { Console.log (this.name); }  }; User.whatisyourname ();//output "Rahul mhatre",var user2 = {Name: "Neha sampat" }; User.whatIsYourName.call (User2);//output "Neha Sampat"

The apply method is similar to the call method. The only difference between the two is that theapply method uses an array to specify the parameters, and each parameter of the call method needs to be specified individually:

    • Apply (Thisarg, [Argsarray])
    • Call (Thisarg, arg1, arg2, ...)

  

var user = {greet: "hello!", GreetUser: function (userName) { Console.log (this.greet + "" + userName); }  };var greet1 = {greet: "Hola" }; User.greetUser.call (Greet1,"Rahul"); //output "Hola Rahul" User.greetUser.apply (Greet1, ["Rahul"]); //output "Hola Rahul"

Using the bind method, you can bind the This value to a function and then return as a new function:

  

var user = {greet: "hello!", GreetUser: function (userName) { Console.log (this.greet + "" + userName); }  };var Greethola = user.greetUser.bind ({greet: "Hola"}); var greetbonjour = user.greetUser.bind ({greet: "Bonjour"}); Greethola ("Rahul") //output "Hola Rahul" Greetbonjour ("Rahul") //output "Bonjour Rahul"   

JavaScript Essentials 1

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.