Relive JavaScript (i)

Source: Internet
Author: User

Work with JavaScript, a group of review notes.

Some views

1. Think of the most commonly used JavaScript host environment, browser or server V8, are single-threaded, so do not think too much concurrency problem, if it is to implement asynchronous method, anyway, it is not to consider the condition of the state.

2. Development methods. Again oop, and OOF, for independent development is to enjoy the play. For collaborative development, sometimes the specification is not very good unity, can be implemented in a way too flexible, dizzy is often the thing, to be easy to understand, naming than writing C # Java language has higher requirements. Anyway often do not understand this, the ghost know is thrown into which environment was called. I think typescript is the solution of the helpless.

3. Performance, each terminal has the environment of each terminal is also listed not complete, the deeper the set, the more pits.

  

Simply think, the most need to focus on JavaScript's OOP grammar model, after all, whether in the browser or server, the other objects are encapsulated, more consideration is the realization of the function of the information model.

Object type

The object type is the base class for all object instances.

hasOwnProperty (PropertyName): Used to check whether a given property exists in the current object instance (not a prototype). The parameter propertyname must be a string.

isPrototypeOf (object): Returns True if the prototype chain of object has a prototype chain of the current instance.

Congruent operator

In addition to comparing the value of the non-conversion operand, congruent and non-congruent operators are no different. The equality operator is represented by 3 equals, and is returned only if the two operands are equal without conversion.

function arguments

The Agruments object of the function is similar to an array, but is accessed using an index, arguments[0], and the length property

Value passing and reference passing

In fact, this is a bit like C #.

1. Copying of basic type values

var num1=5;var num2=num1;

  

2. Reference replication

var obj1=new Object (); var obj2=obj1;obj1.name= "Peter"; alert (obj2.name); Peter

  

3. Passing parameters

The delivery of a primitive type value is like a copy of a primitive type variable, and a reference type is worth passing, like a copy of a reference type variable.

  

function Addtem (num) {  num+=10;  return num;    } var count=20;var result=addten (count); alert (count); 20 No change alert (result); 30

  

function SetName (obj) {  obj.name= "Peter";  } var person=new Object (); SetName (person); alert (person.name); Peter

  

Pit One variable like disorderly jumping field

for (Var i=0;i<10;i++) {  dosomething (i);  } alert (i); 10

function type

Each of these functions is an instance of a function type and has properties and methods just like other reference types. Because a function is an object, the function name is actually a pointer to a function object and is not bound to a function.

function sum (num1,num2) {}var sum=function (num1,num2) {};

  

Pit Two function pointer change

function factorial (num) {    if (num <=1) {        return 1;    } else {        return num * factorial (num-1)}    }            

According to the above function pointer, the name factorial can point to another function, so this function may be dynamically changed at runtime.

The way out of the pit

function factorial (num) {    if (num <=1) {        return 1;        } else {            return num * Arguments.callee (NUM-1)        }}

Arguments.callee is a pointer to the function that owns the arguments object

var truefactorial = factorial;factorial = function () {    return 0;}; Alert (Truefactorial (5)); 120alert (factorial (5)); 0

  

This

If the hierarchy is very round, it is difficult to see clearly.

The summary is, who calls the function, this is pointing to WHO

Window.color = "Red"; var o = {color: "Blue"};function Saycolor () {    alert (this.color);} Saycolor (); "Red" O.saycolor = Saycolor;o.saycolor (); "Blue"

Apply and Call methods

Call a function in a specific scope to set the value of the This object in the function body

Call and apply the first parameter is scope, the following parameter call must be enumerated, apply can use the array

function sum (NUM1, num2) {    return num1 + num2;} function Callsum (NUM1, num2) {    return Sum.call (this, NUM1, num2);} Alert (Callsum (10,10)); 20

Bind method

An instance of the function is created, and this value is bound to the value passed to the BIND function

Window.color = "Red"; var o = {color: "Blue"};function Saycolor () {    alert (this.color);} var objectsaycolor = Saycolor.bind (o); Objectsaycolor (); Blue

  

The difference between encodeURI and encodeURIComponent

encodeURI does not encode special characters that are themselves URIs, such as colons, forward slashes, question marks, and #;encodeuricomponent, which encode any non-standard characters it discovers

var uri = "Http://www.wrox.com/illegal value.htm#start";//"Http://www.wrox.com/illegal%20value.htm#start" alert ( encodeURI (URI));//"Http%3a%2f%2fwww.wrox.com%2fillegal%20value.htm%23start" alert (encodeURIComponent (URI));

  

eval method

When the parser discovers that the eval () method is called in the code, the passed parameters are parsed as the actual ECMAScript statement, and the execution results are inserted into the original location. Code executed through Eval is considered part of the execution environment that contains the call, so the code being executed has the same scope chain as the execution environment. Code that is executed through Eval can reference variables defined in the containing environment

var msg = "Hello World"; eval ("Alert (msg)"); "Hello World"

  

Eval ("function Sayhi () {alert (' Hi ');}"); Sayhi ();

  

Eval ("var msg = ' Hello World ';"); Alert (msg); "Hello World"

  

Relive JavaScript (i)

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.