_javascript techniques of JavaScript knowledge point finishing

Source: Internet
Author: User
Tags closure

JavaScript is a literal translation script language, a dynamic type, a weak type, a prototype based language, and a built-in support type. Its interpreter, known as the JavaScript engine, is widely used as a scripting language for clients, and is used in HTML (an application under standard Universal Markup Language) to add dynamic functionality to HTML Web pages.

First, use the more official text description to explain javascript:

JavaScript is a literal translation script language, a dynamic type, a weak type, a prototype based language, and a built-in support type.
Its interpreter is called the JavaScript engine, a part of the browser, widely used in the scripting language of the client, the first
Used in HTML (an application under standard Universal Markup Language) to add dynamic functionality to HTML Web pages.

JavaScript has some of the following features:

· scripting language. JavaScript is an interpreted scripting language, in which languages such as C and C + + are compiled and executed, and JavaScript is interpreted line by row during the course of the program's operation.
· Object based. JavaScript is an object-based scripting language that can not only create objects but also use existing objects.
· Simple. The JavaScript language is a weakly typed variable type, with no stringent requirements for the data types used, a scripting language based on Java basic statements and controls, and its design is simple and compact.
· Dynamic nature. JavaScript is an event-driven scripting language that can respond to user input without having to go through a Web server. When accessing a webpage, the mouse clicks on the Web page or moves up and down, the window moves and so on the operation JavaScript can directly to these events give the corresponding response.
· Cross-platform nature. JavaScript scripting languages are not dependent on the operating system and require only browser support. As a result, a JavaScript script can be written to use on any machine, on the premise that browsers on the machine support the JavaScript scripting language, and currently JavaScript is supported by most browsers.

JavaScript consists of the following parts:

· ECMAScript, describes the syntax and basic objects of the language.
· The Document Object Model (DOM), which describes methods and interfaces for handling Web page content.
· The browser object model (BOM), which describes the methods and interfaces for interacting with browsers.

What are the data types for JavaScript?

· There are Function, String, Array, object, date, and so on in object
· String
· Number
· Boolean
· Null
· Undefined

The following section of the code to prove that the following:

String, number is an object, string, number is a different data format ...

var str = "ABC";
var Str = new String ("abc");
var num = 100;
var Num = new number (100);
Console.log (typeof str, typeof str, typeof num, typeof num); String Object Number Object

What is an object prototype chain?

When we use a new object (created using a constructor) or create an object with Object.create, the object will have a prototype and a prototype chain.
For example: We create a constructor _obj, and then through the _obj to new one obj, then the prototype chain is: Obj→_obj→object.prototype→null.

Let's explain it by code:

 function _obj () {};
 _obj.prototype.name = "Beast"; Each function contains a prototype attribute that points to a reference to an object that is a "prototype object."
 _obj.prototype.age =;
 _obj.prototype.sayhello = function () {
   console.log ("Hello" +this.name);
 var obj = new _obj ();
 Console.log (obj); _obj{} and its __proto__ contains various attributes of the _obj, including Name,age,sayhello 
 console.log (obj.__proto__ = _obj.prototype);//true
 Obj.sayhello ()//Hello Beast
 

The value of the note is: Object.create (NULL) prototype chain is directly null, which means that the prototype chain of others is very short ...

How do scopes play in JavaScript?

When we use VAR to declare a variable in JavaScript, we actually add a property and a property value to the object that the scope points to.
There is no block-level scope in JavaScript, variables declared within the current scope are also available only within the current scope and within the current scope, and variables declared within the function apply only within that function (if not, if not done), calling the variable outside the function will cause the error not to defined.

Let's run a run code to understand the scope:

 var firstnum = 1;
 ~function () {
   var secondnum = 2;
   Console.log ("Print Inside:" +firstnum, Secondnum); Printed on the inside: 1 2
 } ();
 

Scope also has scope chain:

 var firstnum = 1;
 Console.log ("Print at outermost layer:" +firstnum); Print at outermost: 1
 ~function () {
   var secondnum = 2;
   Console.log ("Print in the middle tier:" +firstnum,secondnum); Printing in the middle tier: 1 2
   ~function () {
     var thirdnum = 3;
     Console.log ("Print in the innermost layer:" +firstnum,secondnum,thirdnum); Print in the innermost layer: 1 2 3
   } ();
   Console.log ("Print in the middle tier:" +firstnum,secondnum,thirdnum); Uncaught referenceerror:thirdnum is not defined (...)
 } ();
 

In other words, the variable declared in the current scope will be in its child ... Koziko scope is still able to use, cool crooked bar, haha ...

What's the closure? How to play?

The execution of a function depends on the scope of the variable, which is determined when the function is defined, not when the function is called. To implement this lexical scope, the internal state of a JavaScript function object contains not only the logical code of the function, but also the current scope chain. Function objects can be interconnected by scope chains, and variables within the function body can be stored within the scope of the function, which is actually a "closure".
Go ahead and look at the code:

function counter () {
  var = 0;
  return {
   count:function () {return num++;},
   reset:function () {return num = 0;}}}
 ;
 var firstnum = counter ();
 var secondnum = counter ();
 Console.log (Firstnum.count ()); 0
 Console.log (secondnum.count ());//0
 Firstnum.reset ();
 Console.log (Firstnum.count ()); 0 num has been reset, so the return is 0
 console.log (secondnum.count ());//1 num is not reset, so return 1
 

Many of the two effects of closures: Reading the value of variables within the function, so that these variable values are always kept (in memory).
At the same time need to note that the closure of the use of caution, do not abuse, not disorderly use, because the function inside the variables are stored in memory, will lead to memory consumption.

This in JavaScript

In JavaScript, this usually points to the function we are executing, or to the object to which the function belongs.

The global this→ is pointing to window
The this→ in the function points to the object where the function is located
The this→ in the object points to its own

Validation code:

 Console.log (this); Window {external:object, Chrome:object, Document:document, Global:window, Cr:object ...} execute console.log globally, so this point is win Dow
 ~function Seethis () {
  console.log (this);//Window {external:object, chrome:object, Document:document, Global:window, Cr:object ...} Since I am writing this function globally, I am pointing to Window
 } ();
 var obj = {
  name: "Beast",
  showthis:function () {
   console.log (this);//Object {name: "Beast", Showthis:function (), __PROTO__} This is the object that is printed here}
 ;
 

Arguments

In a JavaScript function, the arguments is like an array (not a real array), has a length property, and can represent the number of arguments passed to the function.

In simple terms, the actual arguments that are passed when the arguments function executes.
For example: Arguments[0] represents the first argument passed in.

Verifying with Code :

 function Argumentstest () {
  console.log (arguments[0]?arguments[0]: "Hello World", arguments[1]?arguments[1]: "Hi World ")
 };
 Argumentstest (); Hello world.
 

Well, about JavaScript knowledge Point first to all of us, all when the study under the knowledge point, this article is not good, but also hope that many friends advice.

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.