标签:js基本数据类型 类型检测 typeof instanceof constructor
var str = "123,ABC,900,rgy,rrrr"; console.log(str.length);//20 var str = "123,ABC,900,冉光宇,rrrr"; console.log(str.length);//20tip:
var str = "123,ABC,900,冉光宇,rrrr"; console.log(str.toLowerCase());// 123,abc,900,冉光宇,rrrr console.log(str.toUpperCase());// 123,ABC,900,冉光宇,RRRR
var str = "ABC"; console.log(str.charCodeAt(1));// 66 (index == 1的字符B的Unicode编码为66) console.log(String.fromCharCode(65,66,67));// ABC console.log(str.charAt(5));// 空 console.log(str.charAt(1));// B var str = "abcba"; console.log(str.indexOf("b"));// 1 console.log(str.lastIndexOf("b"));// 3 console.log(str.indexOf("z"));// -1 console.log(str.lastIndexOf("z"));// -1 console.log(str.indexOf("b",2));// 3 console.log(str.lastIndexOf("b",4));// 3tip:
var str = "0123456"; var arr = str.substring(1,4); console.log(str);// 0123456 console.log(arr);// 123 console.log(str.substring(4,1));// 123 console.log(str.substring(1));// 123456 console.log(str.substring(-2));// 0123456tip:
var str = "0123456"; var arr = str.substr(1,4); console.log(str);// 0123456 console.log(arr);// 1234tip:
var str = "0123456"; var arr = str.slice(1,4); console.log(str);// 0123456 console.log(arr);// 123 console.log(str.slice(4,1));// 空,不支持这种方式 console.log(str.slice(1));// 123456 console.log(str.slice(-5));// 23456 console.log(str.slice(-5,-1));// 2345tip:
var str = "123,ABC,900,rgy,rrrgyr"; console.log(str.match(/rgy/));// ["rgy", index: 12, input: "123,ABC,900,rgy,rrrgyr"] console.log(str.match(/rgy/g));// ["rgy", "rgy"] tip: 默认匹配第一个,加了标志"g"表示全局匹配 var str = "123,ABC,900,rgy,rrrgyr"; console.log(str.search(/rgy/));// 12 tip: 默认匹配第一个,返回index var str = "123,ABC,900,rgy,rrrgyr"; console.log(str.replace(/rgy/, "kkk"));// 123,ABC,900,kkk,rrrgyr console.log(str.replace(/rgy/g, "kkk"));// 123,ABC,900,kkk,rrkkkr console.log(str.replace(/(\d+),(\w+)/g, "$2,$1"));// ABC,123,rgy,900,rrrgyr var str = "123,ABC,900,rgy,rrrgyr"; var arr = str.split(",");// ["123", "ABC", "900", "rgy", "rrrgyr"] var arr = str.split(/,/);// ["123", "ABC", "900", "rgy", "rrrgyr"] tip: split()方法中参数可以为正则表达式,也可以为其他字符串
console.log(Math.ceil("1000.3"));// 1001(向上取整) console.log(Math.floor("1000.3"));// 1000 (向下取整) console.log(Math.round("1000.3"));// 1000(四舍五入) console.log(parseInt("1000.3"));// 1000 console.log(parseFloat("1000.3"));// 1000.3
console.log(parseInt("1011", 2)); // 11 console.log(parseInt("18", 10));// 18
function Cat(name,color){ this.name = name; this.color = color; } Cat.prototype.type = "猫科动物"; Cat.prototype.eat = function(){ alert("吃老鼠"); } var cat1 = new Cat("大毛","黄色"); var cat2 = new Cat("二毛","黑色"); alert(Cat.prototype.isPrototypeOf(cat1));//true alert(Cat.prototype.isPrototypeOf(cat2));//true alert(cat1.hasOwnProperty("name"));//true alert(cat1.hasOwnProperty("type"));//false
say(); function say(){ alert("hello world!"); }
var nums = [1,2,3,4]; typeof nums // object nums.constructor === Array;// true nums instanceof Array;// true Object.prototype.toString.call(nums);// "[object Array]"
console.log(typeof "123");// "string" console.log(typeof 123);// "number" console.log(typeof {});// "object" console.log(typeof []);// "object" console.log(typeof null);// "object" console.log(typeof true);// "boolean" console.log(typeof function(){});// "function" console.log(typeof undefined);// "undefined"
var obj = { name : "kylin", age : 21, list : [1,2,3] } var Person = function(name,age){ this.name = name; this.age = age; } var p = new Person("kylin", 21); console.log(p instanceof Person); //true console.log(obj instanceof Person); //false console.log(obj instanceof Object); //true console.log(new Date() instanceof Date); //true console.log([] instanceof Array); //true
var Person = function(name,age){ this.name = name; this.age = age; } var p = new Person("kylin", 21); console.log(p.constructor == Person); //true console.log([].constructor == Array); //true console.log(new Date().constructor == Date)//true
Object.prototype.toString.call("123");// "[object String]" Object.prototype.toString.call(123);// "[object Number]" Object.prototype.toString.call({});// "[object Object]" Object.prototype.toString.call([]);// "[object Array]" Object.prototype.toString.call(null);// "[object Null]" Object.prototype.toString.call(true);// "[object Boolean]" Object.prototype.toString.call(function(){});// "[object Function]" Object.prototype.toString.call(undefined);// "[object Undefined]"
标签:js基本数据类型 类型检测 typeof instanceof constructor
原文地址:http://blog.csdn.net/u011700203/article/details/45542535