JS学习笔记(01)—— 基础

来源:互联网 发布:知行论坛北交大 编辑:程序博客网 时间:2024/06/11 16:37
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>01</title>    <script>        // js 的变量类型        var a = 23;        var b = 3.14;        var c = 'hello';        var d = "world";        var e = true;        var f = null;        var g = undefined;        var h = {name:'lisi',  age:29} //对象类型        console.log(h.name);        console.log(h['name']);        var arr = ['a', 3 , 'hello', true]; //序号始终 从0,1, ... ,N,不会空缺        console.log(arr);        console.log(arr[3]);    </script></head><body></body></html> 
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>01</title>    <script>        // js 中,拼接运算符"+"        console.log(2+3);        console.log('hello' + ' ' + 'world');        console.log(2+3+4+'haha'+5+6);//9haha56,一旦碰到非法数字后,后面一律拼接        //js中,逻辑运算,返回的是最早能判断表达式结果的那个值        var a  = false;        var b = 6;        var c = true;        var d =(a || b || c);        console.log(d); // 6        var e = false;        var f =99;        console.log(e && f); // false    </script></head><body></body></html> 

undefined 和 NULL

null表示”没有对象”,即该处不应该有值。典型用法是:
(1) 作为函数的参数,表示该函数的参数不是对象。
(2) 作为对象原型链的终点。

Object.getPrototypeOf(Object.prototype)// null

undefined表示”缺少值”,就是此处应该有一个值,但是还没有定义。典型用法是:
(1)变量被声明了,但没有赋值时,就等于undefined。
(2) 调用函数时,应该提供的参数没有提供,该参数等于undefined。
(3)对象没有赋值的属性,该属性的值为undefined。
(4)函数没有返回值时,默认返回undefined。

var i;i // undefinedfunction f(x){console.log(x)}f() // undefinedvar  o = new Object();o.p // undefinedvar x = f();x // undefined

数组

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>01</title>    <script>        //         var arr = [ '赵', '钱', '孙', '李']; //键已经订死        for (var i = 0; i<arr.length; i++){            console.log(arr[i]);        }        var obj = {name:'list', age:29, area:'bj'};        for( var k in obj)  {       // for循环将会遍历对象的所有属性,把属性赋给k        console.log(k +'~' + obj['k']);        }    </script></head><body></body></html> 
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>01</title>    <script>        var str = 'helloworld';        console.log(str.length);        console.log(str.substr(2,3));        var arr = ['春', '夏' , '秋', '冬'];        console.log(arr.join('!'));        var str = '东,南,西,北';        console.log(str.split(','));    </script></head><body></body></html> 
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>01</title>    <script>        // var str = 'helloworld'; //返回子串的位置,如果没找到返回-1        // alert(str.indexOf('he') >= 0 ? 'find' : 'not find');        var dt = new Date();        console.log(dt.getFullYear());         console.log(Math.floor(2,3)); //Math下的全是静态方法        console.log (Math.random() * 5 + 5); //起始+幅度乘上它    </script></head><body></body></html> 
0 0
原创粉丝点击