zoukankan      html  css  js  c++  java
  • Javascript 变量补充篇

          前面有一篇日志是写道有关JS变量的,这里还写JS变量。看例子:

    <script type="text/javascript">
    		var str='abcde';   
    		var obj=new String(str);
    	
    		function newToString(){
    			return 'Hello world!';
    		}
    		function func(val){
    			val.toString=newToString;
    		}
    		//示例1:传入值
    		func(str);
    		alert(str);
    		//示例2:传入引用
    		func(obj);
    		alert(obj);
    	</script>
    Tips:func(xxx);是修改值的。

          这样如果难看一点,可以看看之前的那个差不多的例子:

    var a = 3.14;  // Declare and initialize a variable
    var b = a;     // Copy the variable's value to a new variable
    a = 4;         // Modify the value of the original variable
    alert(b)       // Displays 3.14; the copy has not changed
    
    var a = [1,2,3];  // Initialize a variable to refer to an array
    var b = a;        // Copy that reference into a new variable
    a[0] = 99;        // Modify the array using the original reference
    alert(b);         // Display the changed array [99,2,3] using the new reference

         变量声明

         变量声明有两种方法:显式和隐式。

         显式一般是指使用Var声明,隐式(即用即声明)

        

    //声明变量
    var str='abc';
    //声明函数
    function foo(){
      str='abc';
    }
    //南明异常对象ex
    try{
      //...
    }
    catch(ex){
      //...
    }
  • 相关阅读:
    ElasticSearch-03-远行、停止
    ElasticSearch-02-elasticsearch.yaml
    Go-31-杂序
    Go-30-main包
    SpringBoot 初入门
    Spring 事务管理
    JDBC Template
    Spring 基于 AspectJ 的 AOP 开发
    Spring AOP 代理
    Spring 的 AOP 概述和底层实现
  • 原文地址:https://www.cnblogs.com/coolicer/p/1848768.html
Copyright © 2011-2022 走看看