作用域

function(){}大括号中的内容是一个作用域;

function 和 var 的声明会被提到作用域的最上面

function f(){

    a = 2;
var b = g();  //此处可以访问到g()函数
a=3;
return b; function g(){  //函数的声明会被提前到作用域顶部
return a;
}
a=1;
} var result = f();
console.log(f());  //
console.log(a)    //3 a未被声明,那么就会在全局作用域被声明;
            // 如果a被在f()中声明,那么全局作用域则访问不到
            // 如果a在全局和局部作用域都被声明:那么两个a互相不干扰

调用函数访问变量的能力

//全局变量
var globalVariable = 'This is global variable'
//全局函数
function globalFunction(){
//局部变量
var localVariable = 'This is local variable'
console.log('visit gloabl/local variable')
console.log(globalVariable)
console.log(localVariable) globalVariable = 'this is change variable' console.log(globalVariable)
//局部函数
function loaclFunction(){
//局部变量
var innerLocalVariable = 'this is inner local variable'
console.log('visit gloabl/local/innerLocal variable')
console.log(globalVariable)
console.log(localVariable)
console.log(innerLocalVariable)
}
//作用域内可访问局部函数
loaclFunction() }
//在全局不能访问局部变量和函数
globalFunction()

上下文

和this关键字有关,是调用当前可执行代码的对象的引用

this指向函数拥有者,只能在函数中使用

this指向构造函数
var pet = {
words:'..',
speak:function(){
console.log(this.words)
console.log(this==pet)
}
} pet.speak()

this指向全局对象
function pet(words){
this.words = words
console.log(this.words)
console.log(this==global)
}
//this指向了全局global对象
pet('..')

this指向实例对象
function Pet(words){
this.words = words
this.speak = function(){
console.log(this.words)
console.log(this)
}
} var cat = new Pet('Miao')
cat.speak();

使用call和apply改变上下文引用对象

this指向引用方法的对象
var pet = {
words:'..',
speak:function(say){
console.log(say + ' ' + this.words)
}
} pet.speak('haha')

使用call-apply
var pet = {
words:'..',
speak:function(say,free){
console.log(say + ' ' + free+ ' '+ this.words)
}
} var dog={
words:'wawa'
} pet.speak.call(dog,'haha','lala')
pet.speak.apply(dog,['haha','lala'])

使用call和apply方便实现继承
function Pet(words){
this.words = words this.speak = function(){
console.log(this.words)
}
}
//Dog继承Pet,拥有了Pet的speak方法
function Dog(words){
Pet.call(this,words)
} var dog = new Dog('wawa') dog.speak()

06慕课网《进击Node.js基础(一)》作用域和上下文的更多相关文章

  1. 03慕课网《进击Node.js基础(一)》API-URL网址解析

    url url.parse(url,query,host);解析域名 url必须,地址字符串 query可选 host 可选:在不清楚协议时正确解析 querystring 字符串和对象之间互相解析 ...

  2. 01慕课网《进击Node.js基础(一)》Node.js安装,创建例子

    版本:偶数位为稳定版本,基数为非稳定版本 - 0.6.x - 0.7.x    - 0.8.x -0.9.x    -0.10.x  -0.11.x 概念:Node.js采用谷歌浏览器的V8引擎,用C ...

  3. 10慕课网《进击Node.js基础(一)》初识promise

    首先用最简单的方式实现一个动画效果 <!doctype> <html> <head> <title>Promise animation</titl ...

  4. 07慕课网《进击Node.js基础(一)》HTTP小爬虫

    获取HTML页面 var http = require('http') var url='http://www.imooc.com/learn/348' http.get(url,function(r ...

  5. 进击Node.js基础(二)

    一.一个牛逼闪闪的知识点Promise npm install bluebird 二.Promise实例 ball.html <!doctype> <!DOCTYPE html> ...

  6. 02慕课网《进击Node.js基础(一)》——CommonJs标准

    是一套规范管理模块 每个js 为一个模块,多个模块作为一个包 node.js和Couchdb是对其的实现: 不同于jQuery 模块:定义.标识.引用(地址/模块名称) 模块类型: 核心模块http ...

  7. 进击Node.js基础(一)

    一.前言 1:Node.js本质上是用chrome浏览器 v8引擎 使用c++编写的JS运行环境 2:相比于JS没有浏览器安全级的限制,额外提供了一些系统级的API:文件读写,进程管理,网络通信等. ...

  8. 04慕课网《进击Node.js基础(一)》HTTP讲解

    HTTP:通信协议 流程概述: http客户端发起请求,创建端口默认8080 http服务器在端口监听客户端请求 http服务器向客户端返回状态和内容 稍微详细解析: 1.域名解析:浏览器搜素自身的D ...

  9. 11慕课网《进击Node.js基础(一)》Buffer和Stream

    Buffer 用来保存原始数据 (logo.png) 以下代码读取logo.png为buffer类型 然后将buffer转化为string,新建png 可以将字符串配置: data:image/png ...

随机推荐

  1. 很清晰的解读i2c协议

    很清晰的解读i2c协议 转载:http://dpinglee.blog.163.com/blog/static/14409775320112239374615/ 1.I2C协议 2条双向串行线,一条数 ...

  2. 以登录实现理解Servlet+jsp+JavaBean开发

    写在前面:菜鸟拙见,望请纠正 学过servlet的都知道,书本上一直说servlet一直作为控制器使用,它不实现view层,也不做具体的事务处理,那servlet到底是干什么的哪?怎么合理的用它呐?? ...

  3. ios开发UI篇--UIButton

    概述 UIButton 是执行自定义代码以响应用户交互的控件. UIButton 其实包含 UIImageView 和 UILabel 两个控件,UIButton 继承于 UIControl,所以有  ...

  4. Mysql数据库 day1

    Mysql数据库属于关系型数据库(mysql.oracle.sql server),非关系型数据库有DB2.Redis MySQL执行原理,逻辑分层.更改数据库处理引擎 作者:Stanley 罗昊 [ ...

  5. u-boot-1.1.6环境变量

    学习目标: 1.分析u-boot-1.1.6环境变量,了解环境变量初始化.设置以及过程 2.为后面能够掌握u-boot-1.1.6如何启动内核过程打下基础 1.环境变量的概念 在分析uboot环境变量 ...

  6. n进制转十进制

    #include<cstdio> #include<iostream> using namespace std; ; int main(){ ,len=; char ch[ma ...

  7. vue打包完样式冲突

    在页面的<style> 后,加上scoped, 例: scoped是实现样式的私有化,使样式不容易被覆盖,不容易被修改,只对当前页面有效

  8. Python_sklearn机器学习库学习笔记(七)the perceptron(感知器)

    一.感知器 感知器是Frank Rosenblatt在1957年就职于Cornell航空实验室时发明的,其灵感来自于对人脑的仿真,大脑是处理信息的神经元(neurons)细胞和链接神经元细胞进行信息传 ...

  9. java面试资源(面试题、面试经验等)

    两年JAVA程序员的面试总结 https://www.cnblogs.com/xuwujing/p/7613084.html 2018JAVA面试题附答案(长期更新) https://blog.csd ...

  10. 【项目管理】 使用IntelliJ IDEA 将项目发布(提交)到GitLab

    https://blog.csdn.net/zsq520520/article/details/51004721 gitlab地址: http://192.168.1.81:200   idea项目p ...