06慕课网《进击Node.js基础(一)》作用域和上下文
作用域
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基础(一)》作用域和上下文的更多相关文章
- 03慕课网《进击Node.js基础(一)》API-URL网址解析
url url.parse(url,query,host);解析域名 url必须,地址字符串 query可选 host 可选:在不清楚协议时正确解析 querystring 字符串和对象之间互相解析 ...
- 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 ...
- 10慕课网《进击Node.js基础(一)》初识promise
首先用最简单的方式实现一个动画效果 <!doctype> <html> <head> <title>Promise animation</titl ...
- 07慕课网《进击Node.js基础(一)》HTTP小爬虫
获取HTML页面 var http = require('http') var url='http://www.imooc.com/learn/348' http.get(url,function(r ...
- 进击Node.js基础(二)
一.一个牛逼闪闪的知识点Promise npm install bluebird 二.Promise实例 ball.html <!doctype> <!DOCTYPE html> ...
- 02慕课网《进击Node.js基础(一)》——CommonJs标准
是一套规范管理模块 每个js 为一个模块,多个模块作为一个包 node.js和Couchdb是对其的实现: 不同于jQuery 模块:定义.标识.引用(地址/模块名称) 模块类型: 核心模块http ...
- 进击Node.js基础(一)
一.前言 1:Node.js本质上是用chrome浏览器 v8引擎 使用c++编写的JS运行环境 2:相比于JS没有浏览器安全级的限制,额外提供了一些系统级的API:文件读写,进程管理,网络通信等. ...
- 04慕课网《进击Node.js基础(一)》HTTP讲解
HTTP:通信协议 流程概述: http客户端发起请求,创建端口默认8080 http服务器在端口监听客户端请求 http服务器向客户端返回状态和内容 稍微详细解析: 1.域名解析:浏览器搜素自身的D ...
- 11慕课网《进击Node.js基础(一)》Buffer和Stream
Buffer 用来保存原始数据 (logo.png) 以下代码读取logo.png为buffer类型 然后将buffer转化为string,新建png 可以将字符串配置: data:image/png ...
随机推荐
- Debian 8 时间同步
每天执行一次 sudo ntpdate ntp.ubuntu.com 逐渐觉得麻烦了,有没有自动执行的方法? 在Linux中用户可以执行例行性的工作,使用crontab这个命令. 步骤: 1.在终端中 ...
- iOS字体相关
1.使用自定义字体 (1)将字体文件导入项目 (2)在info.plist文件中添加 Fonts provided by application (3)获取字体在项目中的名称 for fontFami ...
- 远程连接服务器端Jupyter Notebook
1. 安装 输入命令: sudo apt-get install sshfs 2. 服务器端开启Jupyter Notebook Ubuntu服务器端安装过程参考:www.cnblogs.com/la ...
- TFTP服务的搭建
TFTP服务的作用:提供网络下载服务 tftp服务器的安装与配置: tftp主要用于嵌入式交叉开发环境的搭建,传输文件. 0.创建tftp的工作目录,并修改权限(注意:请在主目录下创建此工作目录!) ...
- 大数据入门第六天——HDFS详解
一.概述 1.HDFS中的角色 Block数据: HDFS中的文件在物理上是分块存储(block),块的大小可以通过配置参数( dfs.blocksize)来规定,默认大小在hadoop2.x版本中是 ...
- 苏州Uber优步司机奖励政策(4月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- python基础学习1-面向对象
#!/usr/bin/env python # -*- coding:utf-8 -*- class Foo:#定义类 def mail(self,email,message):#定义类的方法 pri ...
- P5231 [JSOI2012]玄武密码
P5231 [JSOI2012]玄武密码 链接 分析: 首先对所有询问串建立AC自动机,然后扫描一遍母串,在AC自动机上走,没走到一个点,标记这个点走过了,并且它的fail树上的祖先节点也可以访问到( ...
- 实现后门程序以及相应的rootkits,实现对后门程序的隐藏
iptables的一些命令: a. a) 使用规则实现外网不能访问本机,但是本机主动发起的连接正常进行. sudo iptables –A INPUT -p tcp —tcp —syn -j D ...
- cogs1799 [国家集训队2012]tree(伍一鸣)
LCT裸题 注意打标记之间的影响就是了 这个膜数不会爆unsigned int #include<cstdio> #include<cstdlib> #include<a ...