JavaScript Advanced Extensions

Source: Internet
Author: User


-scope-related
1.
function func () {
if (1==1) {
var v= 123;
}
Console.log (v);
}
Func ()
A. error (java,c#) B. 123 (python, pair) c.undefined
= "javascript/python is scoped with function, non-parenthesis is scope
=" java,c# The brackets are scoped
2.
XO = ' root1 ';
function func () {
var xo = ' Root2 ';
function inner () {
console.log (xo);
}
Inner ();
}
Func ();
Scope chain
//root2
3.

xo = ' root1 ';
function func () {
var xo = ' Root2 ';
function inner () {
console.log (xo);
}
Return inner;
}
result = Func (),
Result (),
//scope chain was created before the function call, and when looking for a variable, find
//root2

4 based on the scope that was created at the Beginning.
XO = ' root1 ';
function func () {
var xo = ' Root2 ';
function inner () {
console.log (xo);
}
var xo = ' root3 '
return inner;
}
XO = "root4"
result = Func ();
Result ();

Root 3

5.
var xxxx;
Console.log (xxxx);


function Func () {
Console.log (xo);
var xo = ' 123 ';
Console.log (xo);
}
Func ()
Declaration in advance, JS Special
1. pre-compilation:
var xo; make xo=undefined;
2. implementation

6.
function Func (num) {
Console.log (num); function
num = 18;
Console.log (num); 18

function num () {
}
Console.log (num); 18
}
Func (666);

A. pre-compiling AO active object Activity objects
Compile the Parameters First:
Ao.num = undefined
Ao.num = 666
To recompile a variable:
If NUM is in the ao, no action is made
otherwise ao.num = undefined
Last Compile Function:
ao.num = function num () {
}

B. Implementation


7.

function Func (num) {
Console.log (num); function
function num () {
}
Console.log (num); function
num = 18;

Console.log (num); 18
}
Func (666);


Compile the Parameters First:
Ao.num = undefined
Ao.num = 666
To recompile a variable:
If NUM is in the ao, no action is made
otherwise ao.num = undefined
Last Compile Function:
ao.num = function num () {
}


8.
function Func () {
Console.log (xo);
var xo = 123;
}
Func ()

Compile:
Parameters:
AO is empty
Variable:
Ao.xo = undefined
Perform:

-functions and object-oriented Correlation
1.
function Func (arg) {
Console.log (this,arg);
}
Func (18);
Func.call (window,20);
Func.apply (window,[30]);


(function (arg) {
Console.log (this,arg);
}) (123)

When the function is executed, the default this is to refer to the Window object

function Func () {
Window.nn = ' Root ';
nn = ' root ';
This.nn = ' Root ';
}
Func ()
Console.log (nn);

=====>
A. Inside the function, the This variable is available by Default. By default, when you execute a function This=window
B. Use the name of the Function. call or function Name. apply can set the value in the

document.getElementById (' ID '). onclick = function () {
This
}

document.getElementById (' ID '). onclick.call (dom Object)


2. Is there a dictionary type in js?
Only objects are forged into dictionary form

var dict = {
Name: ' Alex ',
Age:18
}
Equivalent to
var dict = new Object (); # means creating an empty dictionary
Dict.name = ' Alex ';
Dict.age = 18;



function Foo (name) {
This. Name = Name
}

Foo (' Root ') # As a function, this is the default window
var dict1 = new Foo (' root1 ') # As a class, this is dict1 with the PY self
Foo.call (dict1, ' Root1 ')
var dict2 = new Foo (' root2 ') # As a class, this is the Dict2


====
function Foo (name) {
This. Name = name;
This. Func = function () {
Console.log (this. Name);
}
}
# as a function
Foo (' root1 ')
Window. Name
Window. Func ()

# as a class
obj = new Foo (' root2 ')
Obj. Name
Obj. Func ()


# Direct objects
Dict = {
Name: ' Root3 ',
Func:function () {
Console.log (this. Name);
}
}

# dict = new Object ();
# Dict. Name = ' Root3 ';
# Dict. Func = function () {
Console.log (this. Name);
}
Dict. Func ()
========================== "who called the function, this is who." function () Execution time default WINDOW. function ()


Who calls the function, this is who. function () Execution time default WINDOW. function ()
There is a this in every function
Name = ' 666 ';
var dict = {
Name: ' Root ',
age:18,
Func:function () {
This equals dict
Console.log (this. Name); Root

function Inner () {
Console.log (this. Name); 666
}
Window.inner ();
}
}

Dict. Func ();

============================
Who calls the function, this is who. function () Execution time default WINDOW. function ()
There is a this in every function
Variable Lookup order, scope chain
Name = ' 666 ';
var dict = {
Name: ' Root ',
age:18,
Func:function () {
This equals dict
Console.log (this. Name); Root
That equals Dict.
var = this;

function Inner () {
This=window
Console.log (that. Name); Root
}
Window.inner ();
}
}

Dict. Func ();

3. Prototypes

function Foo (name) {
This. Name = name;
}
Prototype
Foo.prototype = {
Func:function () {
Console.log (this. Name);
}
}

obj1 = new Foo (1)
Obj2 = new Foo (2)
obj3 = new Foo (3)

JavaScript Advanced Extensions

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.