js变量作用域及基本包装类型

本文深入探讨了JavaScript中基本类型与引用类型的复制原理、函数参数传递方式,并详细介绍了Number和String类型的属性与方法,包括如何使用Number的静态属性、String的各种操作方法等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

变量复制的都是栈内存的内容,基本类型复制的是值,引用类型复制的是栈内存里的地址

所有函数的参数都是按值传递的。

基本类型用typeof 检测,引用类型的typeof都是object,所以用instaceof检测具体类型

基本类型不能用instanceof检测

var box=[];
var box1={};
var box2=/dd/;
/*box.instanceof Object;*/

document.write(box instanceof Array);
document.write(box1 instanceof Object);
document.write(box2 instanceof RegExp);

 

Number的静态属性

Number.MAX_VALUE;...

Number.prototype=‘’ ;  //用于增加新的属性和方法

var box=1000;
box.toString();//1000
box.toLocaleString();/1,000
document.write(box.toFixed(2));//1000.12
document.write(box.toExponential());//1.000123456e+3
var box=10008888;
document.write(box.toPrecision());//原值
document.write(box.toPrecision(2));//1.0e+7

String类型

属性:length,constructor,prototype

方法:charAt(m);charCodeAt(m);指定索引位置的字符

var box='this is a box';
var box='1234567890123';
/*document.write(box.length);//13
document.write(box.constructor);//function String() { [native code] }
document.write(box.charCodeAt(2));//105
document.write(box.charAt(2));//i
document.write(box.concat('111','222'));//this is a box111222
document.write(box.slice(4,7));//截取is
document.write(box.substring(4,7));//截取is*/
/*document.write(box.substr(4));//從第四位
document.write(box.substr(4,7));//從第四位,截取7位*/
/*document.write(box.slice(-2));//后2位
document.write(box.slice(-2));//后2位
document.write(box.substring(-2));//負數返回全部字符串*/
//找不到返回-1
document.write(box.indexOf('2',0));//1
document.write(box.lastIndexOf('2',12));//11,从末尾往前搜索,最后一次出现2的位置

 可用于正则的方法

var box="this IS a box";
document.write(box.search('is'));//2
document.write(box.match('is'));//is
document.write(box.split(' '));//以空格分隔,组成数组
document.write(box.toLowerCase());//以空格分隔,组成数组
document.write(box.replace("is","--"));

 



转载于:https://www.cnblogs.com/yestian/p/6501483.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值