Home > Web Front-end > JS Tutorial > Analysis of the most evil function examples in javascript_javascript skills

Analysis of the most evil function examples in javascript_javascript skills

WBOY
Release: 2016-05-16 18:06:16
Original
1061 people have browsed it

This is the most unique function in JavaScript that confuses you
Let’s take a look at the common operations

Copy code Code As follows:

function doit(){
.....
}
doit();

We can use functions in javascript Use it as a method
Copy code The code is as follows:

 var obj=new Object( ; That is, an instance of Function type)




Copy code


The code is as follows:  var result = new Function("num1", "num2", "return num1 num2");
The above execution effect is Same, function result can also be written like this (i.e. function expression)



Copy code


The code is as follows:The only difference between these two ways of writing is that function is executed first, while function expression The formula is not executed until the code is executed. In addition, each function has an array-like arguments object
inside the function execution dynamic parameters, that is,



Copy code


The code is as follows:
arguments are often used in dynamically passing parameters
Since function is an object, it should also have specific attributes



Copy code


The code is as follows:
function person(){ .... } person.name="xxxx"; person.say=function(){
alert(this.name);
}
person.say(); //alert("xxxx")


We also You can think of it as a class, and the function body is equivalent to the constructor




Copy code


The code is as follows: }
}
var p1=new Person("ygm1");
p1.say(); //alert ygm1 ygm1
var p2=new Person("ygm2");
p2. say(); //alert ygm2 ygm2


Note that this.name is used here because this represents the current object. If alert(name) directly seeks the properties of the window object, it will be passed in at the same time. The parameter nm can be used directly in the method say. In fact, this involves a scope chain. Each function body is a scope. The child domain can access the attributes of the parent domain, but not the other way around (in fact, it can also be obtained) , design to closure, some knowledge, no detailed explanation here..)
Compared with some other OO languages, each class can have some static properties or methods, and JavaScript is simulated through prototypes to achieve sharing by each object Its properties



Copy code


The code is as follows:
function Person(num) { . .... } Person.prototype.name = "ygm"; alert(new Person().name);

But the static methods in OO languages ​​are It is called by the class and cannot instantiate itself. In JavaScript, it is exactly the opposite due to its particularity
Pay attention to the name attribute of alertPerson here. If the name is not found in the function body, it will be found in the prototype. If it is found, it will be Mask the name in the prototype and directly return its value
In fact, every time a function is created, a prototype object is also created, and the prototype object refers to object, so object is the base class of all objects
We can override the prototype object
Person.prototype=new ParentPerson();
Person’s prototype object points to the ParentPerson object, and the ParentPerson object points to its own prototype object..., thus forming a prototype chain...
Okay That’s all for today…
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template