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 ...
随机推荐
- UNIX网络编程 卷2 源代码使用
1. 下载源码,W. Richard Stevens的主页:http://www.kohala.com/start/ wget http://www.kohala.com/start/unpv22e/ ...
- 偏前端 - jquery-iframe内触发父窗口自定义事件-
例如父窗口定义了一个事件. top: $(dom1).bind('topEvent', function(){}); 那么iframe里面的元素怎样触发父窗口dom1的事件呢?这样吗? $(dom1, ...
- watir-webdriver 中根据html5中的data-*属性设置元素
def jscript(key="",*hashdict) key_dict=hashdict[0].keys[0] value_dict=hashdict[0][key_dict ...
- vba 批量生成条形图代码
Sub hong3()'' 宏3 宏d Dim a, b As Integer Dim str As String For a = 227 To 947 Step 15 b = a + 5 str = ...
- PostgreSQL timeline
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL基础知识与基本操作索引页 回到顶级页面:PostgreSQL索引页 关于timeline,有如下的说法 http ...
- 使用Python代码处理Excel
转载说明: 原文地址:http://my.oschina.net/alazyer/blog/223354 原文有十处左右的错误,修正后转载于此. 经验证,python 3.4.3下可用.请各位朋友明察 ...
- 2-[Mysql]- 初识sql语句
1.统一字符编码 强调:配置文件中的注释可以有中文,但是配置项中不能出现中文 mysql> \s # 查看字符编码 # 1.在mysql的解压目录下,新建my.ini,然后配置 #mysql5 ...
- Spring Boot:项目打包成war并发布到Tomcat上运行
一.修改pom文件 1. 因为SpringBoot中嵌入的有Tomcat,因此要移除嵌入式的Tomcat插件 <dependency> <groupId>org.springf ...
- MYSQL创建表的约束条件(可选)
一.常用的一些约束条件 一.创建表的完整语法1.创建表的万能模板:create table 库名.表名( 字段名1 类型[(宽度) 约束条件], 字段名2 类型[(宽度) 约束条件], 字段名3 类型 ...
- 日志模块logging介绍
一.日志的级别 日志一般分为5个级别,分别如下: CRITICAL = 50 #FATAL = CRITICAL ERROR = 40 WARNING = 30 #WARN = WARNING INF ...