JavaScript学习

来源:互联网 发布:linux 关闭oracle进程 编辑:程序博客网 时间:2024/06/05 06:07

最近正在学习使用Node.js开发,其中避免不了javascript的使用。在复习javascropt的使用过程中,发现一些以前不了解的知识点。记录一下,后续有新的发现再补充。

1 prototype 属性:每个js对象都有该属性,由js在语法层面没有继承的概念,但这个属性却可以实现类似继承的效果。

    Node.js的源码中,util.js中有一个非常重要的方法  inherits 用于实现类的继承。看一下这个方法的源码:

exports.inherits = function(ctor, superCtor) {  if (ctor === undefined || ctor === null)    throw new TypeError('The constructor to "inherits" must not be ' +                        'null or undefined');  if (superCtor === undefined || superCtor === null)    throw new TypeError('The super constructor to "inherits" must not ' +                        'be null or undefined');  if (superCtor.prototype === undefined)    throw new TypeError('The super constructor to "inherits" must ' +                        'have a prototype');  ctor.super_ = superCtor;  Object.setPrototypeOf(ctor.prototype, superCtor.prototype);};

在W3C中是这样解释的:prototype 属性使您有能力向对象添加属性和方法。http://www.w3school.com.cn/jsref/jsref_prototype_date.asp

object.prototype.name=value

由此,我们可以理解,prototype的作用就是给 对象/类 附加上一些属性或者方法。从效果上达到了继承的效果。


2 call :给个我理解的公式:obj1.func.call(obj)  => ((typeof obj1)obj).func()

详细解释在一篇文章中有不错的说明,这里直接给出链接地址:http://www.jb51.net/article/22963.htm


3 ||和&& 运算符。

    在java,c,c++这些语言中,这两个运算符返回的是一个bool类型的值,但在javascript中却不是这样。

    || : 找到为true的分项就停止,并返回该分项的值,否则执行完并返回最后分项的值;

           var result = item_1 || item_2 || ... || item_n || default;  那么如果item_i中只要有一个的值为true,那么result = item_i(其中i为最小的那个为true的item);否则result = default.

    && : 找到为false的分项就停止,并返回该分项的值,否则执行完并返回最后分项的值;

           var result = item_1 && item_2 && ... && item_n && default;  那么如果item_i中只要有一个的值为false,那么result = item_i(其中i为最小的那个为false的item);否则result = default.






0 0
原创粉丝点击