js语法笔记2




    function print(txt) {
        document.getElementById("container").innerHTML += ('\n') + txt;
    }

   
    console.log = print;
    console.log("hello")


    function fact(num) {
        if (num <= 1) {
            return 1;
        }
        return num * arguments.callee(num - 1);
    }
    print(fact(5))


    window.color = "red";
    function getColor() {
        print(this.color);
    }
    getColor();// red

    var obj = {color: "blue"};
    print("------call---------")
    getColor.call(this);  //red
    getColor.call(window);  //red
    getColor.call(obj);  //blue
    getColor.apply(this) //red
    getColor.apply(obj)  //blue

    print('----bind------')
    var ofun = getColor.bind(obj);
    ofun();//blue


    obj.showColor = getColor;
    obj.showColor();// blue,this即当前调用该方法的对象,即:obj


    function outer() {
        inner();
    }

    function inner() {
        print("主调函数:" + arguments.callee.caller);
    }
    outer();

    function fun(p1, p2, p3, p4) {

    }
    print(fun.length)


    var s1 = "abcd";//基本类型
    s1.name = "abc";
    print(s1.name);

    var s2 = new String("abcd");//包装类型
    s2.name = "abc";
    print(s2.name)


    print('------boolean-----')
    print(new Boolean(false) && true)//true    对象是true
    print(false && true)  //false


    print(typeof new Boolean(false)); //object
    print(typeof new Boolean(true));//object
    print(typeof true);//boolean
    print(typeof false);//boolean

    print(new Boolean(true) instanceof Boolean);//true
    print(true instanceof Boolean);//false


    print(0xff.toString(2))//11111111
    print(0xff.toString(8))//377
    print(0xff.toString(10))// 255

    print(12.345.toFixed(2))//12.35

    print(10.234.toExponential(5))//1.02340e+1
    print(10.235.toExponential(2))//1.02e+1


    print('------string-------')
    print("hello".charAt(0))//h
    print("hello".charCodeAt(0));//104
    print("hello"[0])//h
    print("hello".concat(" world"," !"))//hello world !

    var hello="hello world !";
    print(hello.slice(3))//lo world !
    print(hello.substring(3))//lo world !
    print(hello.substr(3))//lo world !
    print(hello.slice(3,7))//lo w
    print(hello.substring(3,7))//lo w
    print(hello.substr(3,7))//lo worl

    print(hello.toUpperCase())//HELLO WORLD !


    var pattern=/.at/;
    var matchers='cat,bat,sat,fat'.match(pattern)
    print(matchers.index);//0
    print(matchers[0]);//cat
    print(matchers.lastIndex)
    print('cat,bat,sat,fat'.search(/at/))// 1
    print('cat,bat,sat,fat'.replace('at','ond'))//cond,bat,sat,fat
    print('cat,bat,sat,fat'.replace(/at/g,'ond'))//cond,bond,sond,fond
    print('cat,bat,sat,fat'.replace(/(.at)/g,'word$1'));//wordcat,wordbat,wordsat,wordfat






hello
120
red
------call---------
red
red
blue
red
blue
----bind------
blue
blue
主调函数:function outer() {
        inner();
    }
4
undefined
abc
------boolean-----
true
false
object
object
boolean
boolean
true
false
11111111
377
255
12.35
1.02340e+1
1.02e+1
------string-------
h
104
h
hello world !
lo world !
lo world !
lo world !
lo w
lo w
lo worl
HELLO WORLD !
0
cat
undefined
1
cond,bat,sat,fat
cond,bond,sond,fond
wordcat,wordbat,wordsat,wordfat



















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值