js语法笔记2

xiaoxiao2021-02-28  120

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

转载请注明原文地址: https://www.6miu.com/read-21103.html

最新回复(0)