使用charCodeAt计算字节数

//一个字节代表两个汉字,一个字节代表一个英文
//使用charCodeAt计算输出内容字节数
// function test(target) {
// var count = 0;
// for(var i = 0 ; i < target.length ; i++ ){
// if(target.charCodeAt(i) <= 255 ) {
// count++;
// }else if(target.charCodeAt(i) >255 ){
// count += 2;
// }
// }
// console.log(count);
// }
//简洁写发
// function test(target) {
// var a,
// b;
// a = b = target.length;
// for ( var i = 0 ; i < b ; i++ ) {
// if(target.charCodeAt(i) > 255 ) {
// a ++;
// }
// }
// console.log(a);
// }

// , 使用表达式时,用逗号隔开,则计算后面的数值并返回结果
// typeof 变量名 ,未经声明的变量,只有放在typeof不报错(undefined)

posted on 2022-06-26 15:33  爱前端的小魏  阅读(96)  评论(0编辑  收藏  举报

导航