我爱编程

JS基础

2017-06-01  本文已影响0人  figure_ai

比较运算符

   ==   :   等于,只比较值
   ===  :   绝对等于,比较值和类型
   !=   :   不等于,只比较值
   !==  :   不绝对等于,值和类型有一个不相等或者两个都不相等为true

typeof、Null、Undefined

typeof操作符

typeof "John"                // 返回 string 
typeof 3.14                  // 返回 number
typeof false                 // 返回 boolean
typeof [1,2,3,4]             // 返回 object
typeof {name:'John', age:34} // 返回 object

Null

var person = null;           // 值为 null(空), 但类型为对象

Undefined

person = undefined;          // 值为 undefined, 类型是undefined

Undefined和Null的区别

typeof undefined             // undefined
typeof null                  // object
null === undefined           // false
null == undefined            // true

JS类型转换

JS数据类型

constructor属性

"John".constructor                 // 返回函数 String()  { [native code] }
(3.14).constructor                 // 返回函数 Number()  { [native code] }
false.constructor                  // 返回函数 Boolean() { [native code] }
[1,2,3,4].constructor              // 返回函数 Array()   { [native code] }
{name:'John', age:34}.constructor  // 返回函数 Object()  { [native code] }
new Date().constructor             // 返回函数 Date()    { [native code] }
function () {}.constructor         // 返回函数 Function(){ [native code] }

将数字/布尔值/日期转换为字符串

   String(x)         // 将变量 x 转换为字符串并返回
String(false)       // 返回false
String(Date())  // 返回 Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)
Date().toString()
false.toString()
(100 + 23).toString()

将字符串/布尔值/日期转换为数字

   Number("3.14")    // 返回 3.14
   //将布尔值转换为数字
   Number(false)    //返回 0
   Number(true)     //返回 1
   //将日期转换为数字
   d = new Date();
   Number(d)          // 返回 1404568027739
    //日期转换为为数字使用getTime()也有相同的效果
    d.getTime()     //返回 1404568027739

JS正则表达式

正则表达可用于所有文本的搜索和替换操作

   /正则表达式主体/修饰符(可选)
   //runoob是正则表达式主体,i是一个修饰符(搜索时不区分大小写∫)
   var patt = /runoob/i

search()方法使用正则表达式

   var str = "Visit Runoob!"
   var n = str.search("Runoob");
   //输出6

search()方法使用字符串

   var str = "Visit Runoob!"
   var n = str.search("Runoob");
   //输出6

replace()方法使用正则表达式

   //使用正则表达式,不区分大小写将字符串中的Microsoft替换为Runoob
   var str = document.getElementById("demo").innerHTML;
   var txt = str.replace("/microsoft/i", "Runoob");

replace()方法使用字符串

   var str = document.getElementById("demo").innerHTML;
   var txt = str.replace("Mirosoft","Runoob");

正则表达式修饰符

正则表达式模式

RegExp正则表达式对象

test()方法

test()方法是一个正则表达式方法
用于检测字符串是否匹配某个正则表达式。如果字符串中含有匹配的文本,则返回true, 否则返回false

   var patt = /e/;
   patt.test("The best things in life are free!");
   //返回 true

exec()方法

exec()方法是一个正则表达式方法
用于检索字符串中的正则表达式的匹配
该函数返回一个数组,其中存放匹配的结果,如果未找到,则返回值为null

   /e/.exec("The best things in life are free!");
   //输出e

完整的RegExp参考手册

JavaScript RegExp 对象

JS错误-throw、try和catch

try、catch使用实例

   var text = "";
   function message() {
    try {
        addlert("Welcome guest!");
    } catch(err) {
        txt = "本页又一个错误。\n\n"
        txt += "错误描述:" + err.message + ""\n\n;
    }
   }

Throw使用实例

throw语句允许我们创建或抛出异常

   throw exception
   function myFunction() {
    var message, x;
    message = document.getElementById("message");
    message.innerHTML = "";
    x = document.getElementById("demo").value;
    try {
        if (x == "") throw "值为空";
        if (isNaN(x)) throw "不是数字";
        x = Number(x);
        if (x < 5)  throw "太小";
        if (x > 10) throw "太大";
    }
    catch (err) {
        message.innerHTML = "错误:" + err;
    }
   }

JS变量提升

JS 中,函数及变量的声明都将被提升到函数的最顶部。
因此,JS 中,变量可以先使用再声明。

   x = 5; //设置x为5,在此之前并没有对变量x作声明
   
   elem = document.getElementById("demo"); //查找元素
   elem.innerHTML = x;
   
   var x; //在此声明变量x

JS初始化变量,不会提升

   elem = document.getElementById("demo");
   elem.innerHTML = x;
   var x = 7
   //输出结果为Undefin
上一篇 下一篇

猜你喜欢

热点阅读