day8

①使用常量定义字符串和使用new操作符定义字符串的区别?

    var str1 = new String("javascript");//字符串在堆内存中
    var str2 = new String("javascript");

    var str3 = "javascript";//字符串在栈内存中
    var str4 = "javascript";

    console.log( str1 == str2 ); //false
    console.log( str1 == str3 ); //true
    console.log( str3 == str4 ); //true

    //对于string类型,可以当做对象来看待
    //用new关键字创建的字符串对象,是通过引用的方式,在比较的时候是比较引用地址,所以即使字符串相同,两个变量也不同。

②常见的string方法

    var str1 = "google";
    //string.charAt()
    console.log( str1.charAt(3) );//g

    //charCodeAt() ascii码,每个计算机符号所对应的二进制的数字,前256个全世界统一,在这个基础上各个国家作了自己国家字符集扩展。  (GBK > GB2312; big5; unicode--utf-8、utf-16; iso-8859-1)unicode是为了解决全世界字符集的不统一问题而被制订出来。
    console.log( str1.charCodeAt(3) );//103

    //fromCharCode()
    console.log( String.fromCharCode(97) );//"a"

    //substring( star, end ) ps:string是小写开头
    consolo.log( str1.subString( 0, str1.length );//"google"

    //replace() (敏感词替换)(在不使用正则的情况下,只能替换第一个)
    console.log( str1.replace("g", "l");//"loogle"
    var reg = new RegExp("g","g");//第二个参数表示全局环境
    console.log( str1.replace(reg, "l");//"loolle"

    //indexOf() 检索参数第一次出现的位置(正则表达式返回-1)
    console.log( str1.indexOf("o");//1

    //split() 分割字符串,按照指定的字符进行分割,返回一个数组
    console.log( str1.split("o") );//["g", "g", "le"]

③表单验证

④统计字符串每个字符的个数?

字符比对法

对象存储法

⑤Math对象及其常见API

    ##normal
    //Math.ceil 向上取整
    console.log( Math.ceil( 1.1 ) ); //2
    //Math.floor 向下取整
    console.log( Math.floor( 1.9 ) ); //1
    //Math.max 取大值
    console.log( Math.max( 1, 2 ) ); //2
    //Math.min 取小值
    console.log( Math.min( 1, 2 ) ); //1
    //Math.round 四舍五入
    console.log( Math.round( 1.49 ) ); //1
    //Math.pow( x, y ) x的y次方
    console.log( Math.pow( 2, 3 ) ); //8
    //Math.random()随机数
    console.log( Math.random() ); //0-1的随机数
    
    ##三角函数
    //Math.sin();
    console.log( Math.sin( 2Math.PI/360 ) );//1
    //Math.cos();
    console.log( Math.cos( 2Math.PI/350 ) );//0
posted @ 2016-09-05 11:03  Asambojur  阅读(112)  评论(0)    收藏  举报