JavaScript 基本数据类型 与类型检测

来源:互联网 发布:国泰安数据库价钱 编辑:程序博客网 时间:2024/05/18 00:36

一、【JavaScript 基本数据类型】

JavaScript 拥有动态类型。这意味着相同的变量可用作不同的类型
"string"   "number"   "object"   "boolean"   "function"   "undefined"


<1> string类型


属性:
str.length

var str = "123,ABC,900,rgy,rrrr";console.log(str.length);//20var str = "123,ABC,900,冉光宇,rrrr";console.log(str.length);//20
tip:
从实验结果可以看出来不管是英文字符还是汉字字符,它们在length中表现一样,
并没有什么区别,有几个字符就占几个位置,这里并不区分多字节字符,
在这里它们被一视同仁



方法:

转换大小写:
str.toLowerCase();// 返回一个字符串,该字符串中的字母被转换成小写
str.toUpperCase();// 返回一个字符串,该字符串中的字母被转换成大写



var str = "123,ABC,900,冉光宇,rrrr";console.log(str.toLowerCase());// 123,abc,900,冉光宇,rrrrconsole.log(str.toUpperCase());// 123,ABC,900,冉光宇,RRRR


索引字符(字符串):
str.charCodeAt(num);// 返回一个整数,代表指定位置字符的Unicode编码
str.fromCharCode(num2,num2,num3);// 从一些Unicode字符串中返回一个字符串
str.charAt(num);// 指定索引位置处的字符,如果超出有效范围的索引值返回空字符串。 
str.indexOf(flag); // 返回String对象内第一次出现子字符串位置
str.lastIndexOf(flag);// 返回String对象内最后一次出现子字符串位置


var str = "ABC";console.log(str.charCodeAt(1));// 66 (index == 1的字符B的Unicode编码为66)console.log(String.fromCharCode(65,66,67));// ABCconsole.log(str.charAt(5));// 空console.log(str.charAt(1));// Bvar str = "abcba";console.log(str.indexOf("b"));// 1console.log(str.lastIndexOf("b"));// 3console.log(str.indexOf("z"));// -1console.log(str.lastIndexOf("z"));// -1console.log(str.indexOf("b",2));// 3console.log(str.lastIndexOf("b",4));// 3
tip: 
1.indexOf()与 lastIndexOf()若是没有匹配到子字符串,则返回-1
2.indexOf("b",2) 表示由index==2的位置开始,从左向右进行查找
3.lastIndexOf("b",4) 表示由index==4的位置开始,从右向左进行查找


截取字符串:
str.substring(start,end);// 返回位于String对象中指定位置的子字符串
str.substr(start,length);// 返回一个从指定位置开始的指定长度的子字符串
str.slice(start,end);// 返回位于String对象中指定位置的子字符串


var str = "0123456";var arr = str.substring(1,4);console.log(str);// 0123456console.log(arr);// 123console.log(str.substring(4,1));// 123console.log(str.substring(1));// 123456console.log(str.substring(-2));// 0123456
tip:
1.可以看出 substring(1,4)与 substring(4,1)等价,都是截取index == 1 到 index == 4 之间的字符串
2.同时还可以看出它对原数组并无影响
3.参数若为负数,则返回整个完整字符串


////
var str = "0123456";var arr = str.substr(1,4);console.log(str);// 0123456console.log(arr);// 1234
tip:
1.substr(1,4) 表示从index == 1的位置开始截取,向后截取4个字符
2.还可以看出它对原数组并无影响

////
var str = "0123456";var arr = str.slice(1,4);console.log(str);// 0123456console.log(arr);// 123console.log(str.slice(4,1));// 空,不支持这种方式console.log(str.slice(1));// 123456console.log(str.slice(-5));// 23456console.log(str.slice(-5,-1));// 2345
tip:
1.可以看出它对原数组并无影响
2.不支持str.slice(4,1),前面的index大于后面的index
3.支持参数为负数



正则表达式:
match: str.match(reg); // 返回数组
search: str.search(reg); // 返回number
replace: str.replace(reg); // 返回字符串
split: str.split(reg); // 返回数组


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/));// 12tip: 默认匹配第一个,返回indexvar str = "123,ABC,900,rgy,rrrgyr";console.log(str.replace(/rgy/, "kkk"));// 123,ABC,900,kkk,rrrgyrconsole.log(str.replace(/rgy/g, "kkk"));// 123,ABC,900,kkk,rrkkkrconsole.log(str.replace(/(\d+),(\w+)/g, "$2,$1"));// ABC,123,rgy,900,rrrgyrvar 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()方法中参数可以为正则表达式,也可以为其他字符串

<2> number类型


Number.MAX_VALUE // 1.7976931348623157e+308
Number.MIN_VALUE // 5e-324


Number.NEGATIVE_INFINITY // Infinity(无穷大)
Number.POSITIVE_INFINITY // -Infinity(负无穷大)



number转换成string:
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"));// 1000console.log(parseFloat("1000.3"));// 1000.3


parseInt()和 parseFloat():

parseInt(str [, radix]);
parseFloat(str [, radix]);

返回的数都为10进制

radix为可选项,表示进制,该值介于 2 ~ 36 之间,表示以什么进制来进行解析
当参数radix的值为0,或没有设置该参数时,parseInt()会根据string来判断数字的基数。


console.log(parseInt("1011", 2)); // 11console.log(parseInt("18", 10));// 18

parseFloat()用法类似


<3> object类型



属性:
constructor
prototype


方法:
hasOwnProperty(); //用来判断,一个属性是不是本地属性
isPrototypeOf();//用来判断,某个prototype对象和某个实例之间的关系。
toString();
toLocaleString();
valueOf();


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));//truealert(Cat.prototype.isPrototypeOf(cat2));//truealert(cat1.hasOwnProperty("name"));//truealert(cat1.hasOwnProperty("type"));//false

<4> boolean类型


boolean的隐式转换规则:

1.特殊值undefined和null变成false
2.数字0和NaN变成false
3.空字符串变成false
4.所有其他值都变成true



<5> function类型

在js中定义函数的方式有两种:一种是函数声明,另一种是函数表达式


函数声明:
function functionName(arg0, arg1, arg2) {}


函数表达式:
var functionName = function(arg0, arg1, arg2){};


tip:
函数声明时,它有一个重要的特性就是函数声明提升,在执行代码之前会先读取函数声明
因此,可以把函数声明放在调用它的语句后面。
例如:

say();function say(){alert("hello world!");}

JS中没有函数重载的概念,函数调用的时候,会以就近原则来调用


<6> undefined类型


undefined与null的关系:


console.log(undefined == null);// true
console.log(undefined === null);// false


了解更多undefined与null:
http://www.ruanyifeng.com/blog/2014/03/undefined-vs-null.html


判断undefined类型:
if (typeof(value) == "undefined") { 
   alert("undefined"); 
}  


//////////////////////

二、【数据类型检测】


引子:

var nums = [1,2,3,4];typeof nums // objectnums.constructor === Array;// truenums instanceof Array;// trueObject.prototype.toString.call(nums);// "[object Array]"


/////////////////////

<1> typeof



语法:
typeof value;
//支持6个值
"string"   "number"   "object"   "boolean"   "function"   "undefined"


例子:

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"


/////////////////////

<2> instanceof


语法:
value instanceof ?;


例子:

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); //trueconsole.log(obj instanceof Person); //falseconsole.log(obj instanceof Object); //trueconsole.log(new Date() instanceof Date); //trueconsole.log([] instanceof Array); //true

/////////////////////

<3> constructor



语法:
value.constructor === ?;


例子:

var Person = function(name,age){this.name = name;this.age = age; }var p = new Person("kylin", 21);console.log(p.constructor == Person); //trueconsole.log([].constructor == Array); //trueconsole.log(new Date().constructor == Date)//true

/////////////////////

<4> Object.prototype.toString.call()


语法:
Object.prototype.toString.call(value);

例子:

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]"



1 0
原创粉丝点击