项目中的那些事---JavaScript

一、String.charAt(index)

作用:获取字符串指定索引位置的字符

注意:index的值是0~(字符串长度-1)之间的值

<script type="text/javascript">
var str ="my name is javascript";
var c2 = str.charAt(2);
var c3 = str.charAt(3);
var c6 = str.charAt(6);
var c11 = str.charAt(11);
var c15 = str.charAt(15);
console.info("第3个位置的字符是"+c2);
console.info("第4个位置的字符是"+c3);
console.info("第7个位置的字符是"+c6);
console.info("第12个位置的字符是"+c11);
console.info("第16个位置的字符是"+c15);
</script> 运行结果是:
第3个位置的字符是
第4个位置的字符是n
第7个位置的字符是e
第12个位置的字符是j
第16个位置的字符是s

二、String.substring(start[,end])和String.substr(start[,length])

1、String.substring(start[,end])):返回指定开始位置和结束位置之间的字符串

参数所代表的意义

start:字符串起始位置,必选项,是一个非负数。

end:字符串终止位置,可选项,是一个非负数

结果

截取的结果是从start~end-1之间的长度为end-start的字符串,即截取的子串中不包含最后end位置的字符

参数的限制

当start=end时,截取的子串是”“

当start>end时,截取之前会自动交换位置

当start或end为负数时,会自动替换为0

当end不填时会截取从start开始到字符串结尾处的子串

2、String.substri(start[,length])):返回从start开始,长度为length的字符串

参数所代表的意义

start:字符串起始位置,必选项,非负数

length:要截取的子串的长度,可选项,非负数

结果

截取的是从start位置开始,长度为length的子串

参数的限制

当start为负数时,start=str.length+start

当length为0时返回的子串是""

当length不选时返回start到字符串结尾处的子串

下面是代码示例:

 <script type="text/javascript">
testSubstr();
function testSubstr(){
var str = "0123456789";
console.info(str.substring(0));//
console.info(str.substring(3));//
console.info(str.substring(10));//""
console.info(str.substring(12));//""
console.info(str.substring(-2));//
console.info(str.substring(0,5));//
console.info(str.substring(0,9));//
console.info(str.substring(0,10));//
console.info(str.substring(2,7));//
console.info(str.substring(0,-2));//""
console.info(str.substring(3,-4));//
console.info(str.substring(-2,-6));//"" console.info(str.substr(0));//
console.info(str.substr(3));//
console.info(str.substr(10));//""
console.info(str.substr(12));//""
console.info(str.substr(-2));//
console.info(str.substr(0,5));//
console.info(str.substr(0,9));//
console.info(str.substr(0,10));//
console.info(str.substr(2,7));//
console.info(str.substr(0,-2));//""
console.info(str.substr(3,-4));//""
console.info(str.substr(-2,-6));//""
}
</script>

三、typeof(var)函数

作用:检测给定变量的数据类型

JavaScript中的数据类型是弱类型,一个变量var可以用来存放各种数据类型。

下面通过代码来测试:

 function testTypeof(){
console.info(typeof("hello")); //string
console.info(typeof(11)); //number
console.info(typeof(true)); //boolean
console.info(typeof(document)); //object
console.info(typeof(click)); //undefined
console.info(typeof(function head(){})); //function
}
testTypeof(); 浏览器控制台的执行结果如下:
string
number
boolean
object
undefined
function
   function testTypeof(){
console.info("------包含括号的方式------");
console.info(typeof("hello")); //string
console.info(typeof(11)); //number
console.info(typeof(true)); //boolean
console.info(typeof(document)); //object
console.info(typeof(click)); //undefined
console.info(typeof(function head(){})); //function
console.info("------不包含括号的方式------");
console.info(typeof "world"); //string
console.info(typeof 23); //number
console.info(typeof false); //boolean
console.info(typeof onclick); //object
console.info(typeof click); //undefined
console.info(typeof function foot(){}); //function
} testTypeof(); 浏览器控制台输出结果:
------包含括号的方式------ base1.js:48
string base1.js:49
number base1.js:50
boolean base1.js:51
object base1.js:52
undefined base1.js:53
function base1.js:54
------不包含括号的方式------ base1.js:55
string base1.js:56
number base1.js:57
boolean base1.js:58
object base1.js:59
undefined base1.js:60
function
上一篇:python subprocess pipe 实时输出日志


下一篇:CMD 模块定义规范【转】