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>
原文地址:https://www.cnblogs.com/heavyhe/p/4981290.html