Javascript的一些思想

来源:互联网 发布:ad hoc网络协议 编辑:程序博客网 时间:2024/05/16 19:17

1:Javascript对于对象的引用是指向最终对象的。

例子:

    var tem1=new Array("One","Two","Three");

    var tem2=tem1;

    tem1.push("Four");

则有:

    alert(tem1!==tem2);

因为:

    tem2一开始就顺着tem1指向了new Array("One","Two","Three");

而且:

    在Javascript中,对某个对象进行修改,比如Array的push操作会新建对象。

   哪怕是操作str1+="added"; str1也不再是旧对象而是新对象了,因此会有:

    var str1="str";

    var str2=str1;

    str1+="added";

则有:

    alert(str1!==str2);

 2:函数重载

     Javascript中没有重载的概念,只要是函数名相同,参数个数不同无所谓。但是可以采用合适的办法做到这一点。方法就是利用每个函数都会有的变量:arguments.它类似于一个伪数组,但它是只读的,不能修改,如push。它包含了每个函数的所有传递参数,参数的个数就是arguments.length。

   此处有一个很好的例子特此记录:

   function sendmessage(message,obj)

{

if (arguments.length==2)

{

  obj.handle(message) 

}

}

调用很经典:

    sendmessage("message",{

handle:function

{

alert("Other message");

}

}

3:类型检查

   两种方式:typeof和constractor

Variable            typeofVariable             Variable.constructor

{an:"object"}         object                            Object
["an","array"]          object                             Array
function(){}           function                         Function
"a string"                string                             String
55                         number                          Number
true                       boolean                         Boolean
new User()              object                              User

NaN                    number

 

 有以上分析可知:constructor的判断更为精细,只是类型的首字母大写

4:不带 var 的变量都是全局变量,全局变量都是window对象的属性

5:javascript的函数是运行在执行域的,而不是在运行域。

6:javascript是有预编译的功能的,预编译是以段为处理单元的,它在函数初加载的时候会把带 var 和function的变量初始化为undefinded,变量的值会在运行的时候赋予。

4,5,6的例子:

例子1:

<script> alert(typeof eve); //结果:function alert(typeof walle); //结果:undefined function eve() { //函数定义式  alert('I am Laruence'); }; var walle = function() { //函数表达式 } alert(typeof walle); //结果:function</script> 

例子2:

<script> alert(typeof eve); //结果:undefined</script><script> function eve() {  alert('I am Laruence'); }</script>
原创粉丝点击