06 MU class Network "Attack on node. JS Foundation (i)" Scope and context

Source: Internet
Author: User

Scope

Ability to invoke a function to access a variable

//Global VariablesvarGlobalVariable = ' This is global variable '//Global Functionsfunctionglobalfunction () {//Local Variables    varLocalvariable = ' This is local variable 'Console.log (' Visit gloabl/local variable ') Console.log (globalvariable) console.log (localvariable) globalvariable= ' This was change variable 'Console.log (globalvariable)//Local Functions    functionloaclfunction () {//Local Variables        varInnerlocalvariable = ' This is inner local variable 'Console.log (' Visit gloabl/local/innerlocal variable ') Console.log (globalvariable) console.log (localvariable) Console.log (innerlocalvariable)} //Accessible local functions within a scopeloaclfunction ()}//local variables and functions cannot be accessed globallyGlobalfunction ()

Context

Related to the This keyword, is a reference to an object that invokes the current executable code

This points to the function owner and can only be used in functions

This points to the constructor function
var pet = {    words:' ... ' ,    speak:function() {        Console.log (this. Words)        Console.log (  This = =pet)    }}pet.speak ()

This points to the global object
function Pet (words) {    this. Words = words    console.log (this. words)    Console.log (this = = Global)}//This points to the global globally object Pet (' ... ')

This points to the instance object
function Pet (words) {    this]words = words    thisfunction() {        Console.log (this. words)        Console.log (this)    }}  varNew Pet (' Miao ') cat.speak ();

Using call and apply to change the context reference object this points to the object referencing the method
var pet = {    words:' ... ' ,    speak:function(say) {        this. Words)    }}pet.speak (' haha ‘)

Using call-apply
var pet = {    words:' ... ' ,    speak:function(say,free) {        this. Words    }}var dog={    words:' Wawa '}pet.speak.call (dog,' haha ', ' lala ') pet.speak.apply (dog, [' haha ', ' lala '])

Use call and apply for inheritance convenience
function Pet (words) {    this]words = words    thisfunction() {        Console.log (this. words)    }}//Dog Inherits Pet, owning the Speak method of Pet function  Dog (words) {    Pet.call (this, words)}varNew Dog (' Wawa ') dog.speak ()

06 MU class Network "Attack on node. JS Foundation (i)" Scope and context

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.