关于 JS -基本用法

JS 基本用法

  1.  typeof
     返回变量类型
     var a = 33 
     console.log(typeof a)  // number
    
  2.  instanceof    A instanceof B  检测B.prototype是否存在于A的原型链上。
     判断一个对象是不是另外一个对象的实例
     简单用法:
         function Person(){};
         var p =new Person();
         console.log(p instanceof Person);//true
     复杂用法:
         function Person() {}
         console.log(Object instanceof Object); // true
         //第一个Object的原型链:Object=>
         //Object.__proto__ => Function.prototype=>Function.prototype.__proto__=>Object.prototype
         //第二个Object的原型:Object=> Object.prototype    
    
  3. 字符串方法:
       1.查找字符串中的字符串
           indexOf()
           lastIndexOf()
           search() //方法返回字符串中指定文本第一次出现的位置
       2. 截取字符串长度
           slice(start, end)
           substring(start, end)
           substr(start, length)
       3. replace() 替换
       4. 大小写
           toLowerCase() 
           toUpperCase()    // 使用正则表达式 /i(大小写不敏感)
       5. 链接字符串 
           concat()
       6. 首尾去空格
           trim()
       7. 提取字符串字符
           charAt(position)   
           var str = "HELLO WORLD";
           str.charAt(0);  // 返回 H
           charCodeAt(position) 
           // 方法返回字符串中指定索引的字符 unicode 编码:
       8. 把字符串转换为数组
           split()
           var str = "Hello";
           var arr = str.split("");  // ["H", "e", "l", "l", "o"]

猜你喜欢

转载自www.cnblogs.com/howar/p/12982827.html