nodejs 常识&概念
2017-03-17 15:11
225 查看
全局变量
global
global是全局对象。所有在node进程里的对象都挂在global上。global和BLOBAL看起来是一个同一个东西且是一回事。确实,Global是global一个别名。
定义一个全局变量
global.name = 'xm' Global.name = 'xm' name = 'xm'
删除全局变量
delete global.name
几个常用全局变量
console.log(__dirname);//当前路径 console.log(__filename);//当前在执行的js文件路径 console.log(process.env);//环境变量 process.on('exit', function(code) { // 以下代码永远不会执行 setTimeout(function() { console.log("该代码不会执行"); }, 0); console.log('退出码为:', code); }); console.log("程序执行结束");
模块内全局变量
如果使用var关键字声明的变量将会保留在本地模块里;这些声明的变量没有附加到global对象里。var company = 'Yahoo';
其他模块引用模块内变量
var company = require('./main').company; console.log(company);
因此一个包体中,有2钟创建node全局变量的方法,一个是使用global对象,另一个是使用modules.exports。哪个是我推荐的呢?global方法适用小的应用,modules.exprots适用于大的应用。
模块导入require
引用(include)一个已经被其他模块引用过的模块时,仅仅创建一个指向之前包体的引用,因此这意味着不会极度消耗内存。也因为没有重新创建一个真正的包体,在module里的所有初始化方法没有再执行。this
this关键字是在真正被执行到的时候才会发挥作用。this对象仍然是指代函数被执行时执行该函数的对象。下面分四种情况,详细讨论this的用法
情况一:纯粹的函数调用
这是函数的最通常用法,属于全局性调用,因此this就代表全局对象Global。x=1; function test(){ console.log(this.x);//=>1 x=2; } test(); // 1 console.log(x);//=>1
情况二:作为对象方法的调用
函数还可以作为某个对象的方法调用,这时this就指这个上级对象。function test(){ console.log(this.x); } var o = {}; o.x = 1; o.m = test; o.m(); // 1
情况三 作为构造函数调用
构造函数,就是通过这个函数生成一个新对象(object)。这时,this就指这个新对象。function test(){ this.x = 1; } var o = new test(); console.log(o.x); // 1
情况四 apply/call调用
apply()是函数对象的一个方法,它的作用是改变函数的调用对象,它的第一个参数就表示改变后的调用这个函数的对象。因此,this指的就是这第一个参数。x = 0; function test(){ console.log(this.x); } var o={}; o.x = 1; o.m = test; o.m.apply(); //0
apply()的参数为空时,默认调用全局对象。因此,这时的运行结果为0,证明this指的是全局对象。
如果把最后一行代码修改为
o.m.apply(o); //1
运行结果就变成了1,证明了这时this代表的是对象o。
情况五:闭包下
var object = { name : "My Object", getNameFunc : function(){ return function () { return this.name; }; } }; name = "global"; console.log(object.getNameFunc()());//=》global 类似于情况一,闭包下调用this为全局变量 var obj = { name : "last", myf : object.getNameFunc() }; console.log(obj.myf());//=》last把object.getNameFunc()赋值给myf是,实际等价于将函数function (){ return this.name; }
call与apply
两个方法的调用作用都是讲一个函数的对象上下文从初始的上下文改变为指定的新对象。call(thisObj, arg1, arg2, ...) apply(thisObj, argArray)
实例:
函数对象替换
function add(a,b) { console.log(a+b); } function sub(a,b) { console.log(a-b); } add.call(sub,3,1);//=>4
对象多态性
function Animal(){ this.name = "Animal"; this.showName = function(){ console.log(this.name); } } function Cat(){ this.name = "Cat"; } var animal = new Animal(); var cat = new Cat(); //通过call或apply方法,将原本属于Animal对象的showName()方法交给对象cat来使用了。 //输入结果为"Cat" animal.showName.call(cat,","); //animal.showName.apply(cat,[]);
对象继承
function Animal(name){ this.name = name; this.showName = function(){ console.log(this.name); } } function Cat(name){ Animal.call(this, name); } var cat = new Cat("Black Cat"); cat.showN ba2d ame();
多重继承
function Class10() { this.showSub = function(a,b) { console.log(a-b); } } function Class11() { this.showAdd = function(a,b) { console.log(a+b); } } function Class2() { Class10.call(this); Class11.call(this); } new Class2().showAdd(1,2);//=>3
arguments
arguments是函数的实际参数对象function test(a,b,c) { console.log(arguments); } test(1,2,3);//=>{ '0': 1, '1': 2, '2': 3 }
将函数的实际参数转换成数组的方法
方法一:var args =Array.prototype.slice.call(arguments);
方法二:
var args = [].slice.call(arguments, 0);
方法三:
var args = []; for (var i = 1; i < arguments.length; i++) { args.push(arguments[i]); }
未完…
相关文章推荐
- GDI 基本概念 & 窗口的绘制
- MPEG4 & H.264学习笔记之二 ------ 视频编码基本概念及时域模型
- [概念] 激励理论 - 马斯洛需求层次理论(Maslow's hierarchy of needs)
- LINK : fatal error LNK1104: cannot open file "Debug/工程.exe"(小的常识)
- IBM"智慧地球"概念并不成熟
- 5.3 AOP的3个关键概念 & 5.4 Spring的3种切入点(Pointcut)实现
- ASP+配置 &#8212; ASP+配置概念(三)
- 极浅显编序号常识凸显有序号数n>一切整数
- MPEG4 & H.264学习笔记之二 ------ 视频编码基本概念及时域模型
- Remoting基本概念&牛刀小试
- [概念] 激励理论-海兹伯格激励理论(Herzberg's theory of motivation)
- [概念] 激励理论-海兹伯格激励理论(Herzberg's theory of motivation)
- [概念] 激励理论-海兹伯格激励理论(Herzberg's theory of motivation)
- [概念] 激励理论 - 马斯洛需求层次理论(Maslow's hierarchy of needs)
- WCDMA缩略语 & 几个容易混淆的概念 & 关于掉话原因分析
- [概念] 激励理论 - 马斯洛需求层次理论(Maslow's hierarchy of needs)
- 集群(cluster)&高可用性(HA)概念
- ASP+配置 &#8212; ASP+配置概念(二)
- 锁概念 以及 例程说明"脏读"."不可重复的读"及"虚读"
- Box容器中的"支架"概念