JavaScript學習----基礎知識

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>JavaScript Study  Basic 2015.11.20--</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">  

    </style>
    
    <script type="text/javascript">
    
     //JavaScript = ECMAScript(function,closure,OO) + DOM + BOM
    
    //js基礎

    //ECMAScript 有 5 種原始類型(primitive type),即 Undefined、Null、Boolean、Number 和 String。
    //對變量或值調用 typeof 運算符將返回下列值之一:
    //undefined - 若是變量是 Undefined 類型的
    //boolean - 若是變量是 Boolean 類型的
    //number - 若是變量是 Number 類型的
    //string - 若是變量是 String 類型的
    //object - 若是變量是一種引用類型或 Null 類型的
    
    var a="hello";
    document.write(typeof a +"<br>");//string
    a = 10;
    document.write(typeof a+"<br>");//number
    a = 10.6;
    document.write(typeof a+"<br>");//number
    a = function(){}
    document.write(typeof a+"<br>");//function *
    a = new Object();
    document.write(typeof a+"<br>");//object
    a = undefined;
    document.write(typeof a+"<br>");//undefined *
    a = NaN;
    document.write(typeof a+"<br>");//number *
    a = null;
    document.write(typeof a+"<br>");//object *
    a = true;
    document.write(typeof a+"<br>");//boolean
    a= -1;
    document.write(typeof a+"<br>");//number
    a= [1,2,"3"];
    document.write(typeof a+"<br>");//object
    
    document.write(null == undefined +"<br>");  //輸出 "true" *
    
    //NaN *
    
    //最後一個特殊值是 NaN,表示非數(Not a Number)。NaN 是個奇怪的特殊值。
    //通常說來,這種狀況發生在類型(String、Boolean 等)轉換失敗時。
    //例如,要把單詞 blue 轉換成數值就會失敗,由於沒有與之等價的數值。
    //與無窮大同樣,NaN 也不能用於算術計算。
    //NaN 的另外一個奇特之處在於,它與自身不相等,這意味着下面的代碼將返回 false:
    document.write(NaN == NaN+"<br>");  //輸出 "false" *
    //出於這個緣由,不推薦使用 NaN 值自己。函數 isNaN() 會作得至關好:
    document.write(isNaN("blue")+"<br>");  //輸出 "true"  *
    document.write(isNaN("666")+"<br>");  //輸出 "false"  *
    
    
    //*
    var iNum1 = parseInt("12345red");    //返回 12345  *
    document.write(iNum1+"<br>");
    var iNum1 = parseInt("0xA");    //返回 10
    document.write(iNum1+"<br>");
    var iNum1 = parseInt("56.9");    //返回 56
    document.write(iNum1+"<br>");
    var iNum1 = parseInt("red");    //返回 NaN  *
    document.write(iNum1+"<br>");

/*
    Object 對象
Object 對象自身用處不大,不過在瞭解其餘類以前,仍是應該瞭解它。由於 ECMAScript 中的 Object 對象與 Java 中的 java.lang.Object 類似,ECMAScript 中的全部對象都由這個對象繼承而來,Object 對象中的全部屬性和方法都會出如今其餘對象中,因此理解了 Object 對象,就能夠更好地理解其餘對象。
Object 對象具備下列屬性:
constructor
對建立對象的函數的引用(指針)。對於 Object 對象,該指針指向原始的 Object() 函數。
Prototype
對該對象的對象原型的引用。對於全部的對象,它默認返回 Object 對象的一個實例。
Object 對象還具備幾個方法:
hasOwnProperty(property)
判斷對象是否有某個特定的屬性。必須用字符串指定該屬性。(例如,o.hasOwnProperty("name"))
IsPrototypeOf(object)
判斷該對象是否爲另外一個對象的原型。
PropertyIsEnumerable
判斷給定的屬性是否能夠用 for...in 語句進行枚舉。
ToString()
返回對象的原始字符串表示。對於 Object 對象,ECMA-262 沒有定義這個值,因此不一樣的 ECMAScript 實現具備不一樣的值。
ValueOf()
返回最適合該對象的原始值。對於許多對象,該方法返回的值都與 ToString() 的返回值相同。
註釋:上面列出的每種屬性和方法都會被其餘對象覆蓋。
    
    */
    
    
    
    var obj = new Object();
    document.write(obj.constructor);//function Object() { [native code] }
    document.write("<br>");
    
    document.write(obj.prototype);//undefined
    document.write("<br>");
        
    document.write(obj.toString());//[object Object]
    document.write("<br>");
        
    var obj = {
        name:"li",
        age:20,
        say:function(){
            alert(this.name+","+this.age);
        }
    }
    
    document.write(obj.constructor);//function Object() { [native code] }
    document.write("<br>");
    
    document.write(obj.prototype);//undefined
    document.write("<br>");
        
    document.write(obj.toString());//[object Object]
    document.write("<br>");
        
    
    function Person(name,age){
      this.name = name;
      this.age = age;
      this.say= function(){
         alert(this.name+","+this.age);
      }
    }
    
    var obj = new Person("li",20);
    
    document.write(obj.constructor);//function Person(name,age){ this.name = name; this.age = age; this.say= function(){ alert(this.name+","+this.age); } }
    document.write("<br>");
    
    document.write(obj.prototype);//undefined
    document.write("<br>");
        
    document.write(obj.toString());//[object Object]
    document.write("<br>");
    
    document.write(obj.hasOwnProperty("name"));//true
    document.write("<br>");

    
    
    //Boolean對象
    var oFalseObject = new Boolean(false);
    var bResult = oFalseObject && true;    //輸出 true
    
    document.write(oFalseObject);//false
    document.write("<br>");
    
    document.write(bResult);//true
    document.write("<br>");
    //Number對象
    var oNumberObject = new Number(68);
    document.write(oNumberObject.toFixed(2));  //輸出 "68.00"
    document.write("<br>");

    //string對象
    var oStringObject = new String("hello world");
    //String 對象的 valueOf() 方法和 toString() 方法都會返回 String 類型的原始值:
    document.write(oStringObject.valueOf() == oStringObject.toString());    //輸出 "true"
    document.write("<br>");
    document.write(oStringObject.length);    //輸出 "11"
    document.write("<br>");
        
        

        
    document.write(oStringObject.charAt(1));// e
    document.write("<br>");
    document.write(oStringObject.charCodeAt(1));//101
    document.write("<br>");
        
    //contact    
    var oStringObject = new String("hello ");
    var sResult = oStringObject.concat("world");
    //alert(sResult);        //輸出 "hello world"
    //alert(oStringObject);    //輸出 "hello "

    document.write(oStringObject.indexOf("o"));        //輸出 "4"
    document.write("<br>");
    document.write(oStringObject.lastIndexOf("o"));    //輸出 "7"
    document.write("<br>");
        
        
    document.write(oStringObject.slice("3"));        //輸出 "lo world"
    document.write("<br>");
    document.write(oStringObject.substring("3"));        //輸出 "lo world"
    document.write("<br>");
    document.write(oStringObject.slice("3", "7"));        //輸出 "lo w"
    document.write("<br>");
    document.write(oStringObject.substring("3", "7"));    //輸出 "lo w"
    document.write("<br>");
    document.write(oStringObject instanceof String);    //輸出 "true"
    
    document.write("<br>");

        
    //全等號由三個等號表示(===),只有在無需類型轉換運算數就相等的狀況下,才返回 true。
        
    var sNum = "66";
    var iNum = 66;
    document.write(sNum == iNum);    //輸出 "true"
    document.write(sNum === iNum);    //輸出 "false"    
    /*
    表達式    值
    null == undefined    true
    "NaN" == NaN    false
    5 == NaN    false
    NaN == NaN    false
    NaN != NaN    true
    false == 0    true
    true == 1    true // 1爲true, 非1都爲false
    true == 2    false
    undefined == 0    false
    null == 0    false
    "5" == 5    true
    
    */
    




    </script>
  </head>
  <body>
    <div id="wrap">
      
    </div>
  </body>
</html>
相關文章
相關標籤/搜索