js标准库Number-String-Math-Date(深入)

复制代码
  1 // js标准库,计算js内置的对象,可以直接使用。
  2 // 数值,字符串,布尔类型,在使用的时候会被对应的原始数据类型包装,组成包装对象。
  3 //静态方法:只能在以原生对象调用,不能通过实列调用
  4 //实例方法:由该构造函数创建的对象,都可以使用的方法
  5 
  6 //一、Number  对象,也是一个构造函数
  7 //1.Number可以作为构造函数使用
  8 var n = new Number(1)
  9 console.log(n);
 10 //2.Number可以作为一个普通函数使用,可以把任意数据类型转换为数值类型,null,"",false怎样转换为0,如果有不能装换成数值类型的,则转换成NaN
 11 // parseInt是转为整数,Number是转为数值类型,比parseInt更严谨。
 12 var num1 = '12.1'
 13 var num2 = Number(num1);
 14 console.log(num2, typeof (num2));//12.1 "number"
 15 
 16 //3.toString()把number类型转换成字符串类型
 17 var num1 = 10;
 18 var num2 = num1.toString()
 19 console.log(num2, typeof (num2));//10 string
 20 
 21 //4.toFixed()将一个数转为指定位数的小数,返回小数对应的字符串,如果多位小数,会按四舍五入的方式转,精度会有问题
 22 var num1 = 12.146
 23 var num2 = num1.toFixed(2)
 24 console.log(num2, typeof (num2));//12.15 string
 25 
 26 //5. 自定义方法,在Number对象的原型上定义方法
 27 Number.prototype.add = function (num) {
 28     console.log(this);
 29 
 30     return this + num;
 31 }
 32 var num1 = 3;
 33 console.log(num1.add(10));
 34 /* 
 35 Number {3}
 36  __proto__: Number
 37  [[PrimitiveValue]]: 3
 38  13
 39   */
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61  //二、String对象  js的标准库  包含对字符串的堆操作,所有的字符串的原型都是String(所有字符串都是String的对象)
 62  //1、String也可以作为一个构造函数,返回String对象
 63 var str = new String('abc')
 64 console.log(str);
 65  /* 
 66 
 67 String {"abc"}
 68 0: "a"
 69 1: "b"
 70 2: "c"
 71 length: 3
 72 __proto__: String
 73 [[PrimitiveValue]]: "abc"
 74 
 75  */
 76 //2.fromCharCode(code)  //可以返回unicode编码(码点)对应的字符(静态方法)
 77 console.log(String.fromCharCode(97, 98, 99));//abc
 78 
 79 //3、length属性,返回字符串的长度
 80 var str1 = "abcdef"; // js运行时是通过String构造函数创建
 81 console.log(str1.length);//6
 82 
 83 
 84 //4。concat()连接两个字符串,返回一个新的字符串,不会改变原来的字符串
 85 console.log(str1.concat('123'));//abcdef123
 86 
 87 
 88 //5.字符串使用时会被包装为字符串对象,每一个字符都有一个索引值,从0开始
 89 console.log((str1[0]));//a
 90 
 91 
 92 //6.slice(startIndex  endIndex),substr()和slice一致
 93 var str1 = 'abcdefcd'
 94 console.log(str1.slice(1, 4));//bcd
 95 console.log(str1.slice(-4, -1));//efc  小的索引值在前
 96 
 97 
 98 //7.indexOf(str)返回一个字符在字符串中第一次出现的位置(索引)。从左往右
 99 console.log(str1.indexOf('c'));//2
100 
101 
102 
103 //8.lastIndexOf()返回一个字符在字符串中第最后一次出现的位置(索引)。从右往左
104 console.log(str1.lastIndexOf('c'));//6
105 
106 
107 
108 //9.trim()去掉字符串两端的空格
109 var str1 = '                             abc '
110 console.log(str1.trim());
111 
112 
113 //10.将字符串转换成小写
114 var str4 = "Hello Worlld";
115 // 11. toUpperCase(); 讲字母转为大写
116 console.log(str4.toLowerCase(),str4.toUpperCase());
117 
118 
119 
120 
121 // 12. match() 用于匹配字符中是否匹配参数字符串,如果匹配成功立刻返回(不会再往后查询)匹配字符串数组,不成功则返回null *
122 console.log(str4.match('l'));//["l", index: 2, input: "Hello Worlld", groups: undefined]
123 console.log(str4.match('ll'));//["ll", index: 2, input: "Hello Worlld", groups: undefined]
124 
125 
126 //13. search() 用于匹配字符串中参数字符串第一次出现的位置,如果没有匹配,则返回-1*
127 console.log(str4.search('lll'));//-1
128 console.log(str4.search('l'));//2
129 
130 
131 
132 //14.replace(str1,str2)用str2替换str1中匹配的字符串,一般只会匹配一次*
133 console.log(str4.replace('ll', '--'));//He--o Worlld
134 
135 
136 //15.split(str)按参数字符分割成数组(字符串转数组的方法)join()是数组转字符串的方法*
137 //如果没有找到参数字符,则把整个字符放到数组里面去
138 //// 如果是空字符,就把字符串每个字符作为数组的元素。
139 var str="hi-how-are-you";
140 console.log(str.split(''));//(14) ["h", "i", "-", "h", "o", "w", "-", "a", "r", "e", "-", "y", "o", "u"]
141 console.log(str.split('-'));//(4) ["hi", "how", "are", "you"]
142 console.log(str.split());//["hi-how-are-you"]
143 
144 
145 // 16. localCompare() 比较两个字符串,返回一个整数,小于0则是第一个字符串小于第二字符串,0则是两个字符串相等,大于0表示第一个字符串更大。
146 // 内部才用的是unicode编码的比较。
147 console.log("ab".localeCompare('ab'));//0
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 // 三、Math对象,提供了各种数学功能,不是一个构造函数,不能产生实例。
170 //1.Math.abs();返回绝对值
171 var num = -1;
172 console.log(Math.abs(num));//1
173 
174 
175 //2.max(a,b,c)返回参数中的最大值
176 console.log(Math.max(3, 2, 5));//5
177 
178 
179 // 3. min(a,b,c) 返回参数中最小的值
180 console.log(Math.min(1, 3, 9, 7));//1
181 
182 
183 //4.floor(num)返回小于或等于参数值的最大值(地板值,下舍入)
184 console.log(Math.floor(3.8));//3
185 
186 
187 // 5. ceil(num) 返回大于或等于参数值的最小整数(上舍入);
188 console.log(Math.ceil(3.3));//4
189 
190 
191 //6.round()四舍五入
192 console.log(Math.round(3.8));//4
193 
194 
195 // 7. pow() 幂运算
196 console.log(2 ** 3);//8
197 console.log(Math.pow(2, 3));//8
198 
199 // 8. sqrt() 平方根
200 console.log(Math.sqrt(4));
201 
202 // 9. random() 返回0-1之间的随机数,可以等于0,但是不等于1。
203 var num4 = Math.random()
204 // 9.1 产生1-3的整数 
205 console.log(Math.floor( Math.random()*(3-1)+1));
206 // 9.2 封装产生范围内的随机整数方法
207 function randomInt(a,b) {
208     return Math.floor(Math.random()*(b-a)+a)
209 }
210 var num4 = randomInt(1, 5)
211 console.log(num4);
212 // 9.3 写一个产生随机的4位验证码的方法
213 function randomNum() {
214     var str = ''
215     for (let i = 0; i <4; i++) {
216         // str+=Math.floor(Math.random()*(10-1)+0)
217         str+=randomInt(0,10)
218         
219     }
220     return str
221 }
222 console.log(randomNum());
223 
224 
225 
226 
227 
228 
229 
230 
231 
232 
233 
234 
235 
236 
237 
238 //四、日期对象Date(),是一个构造函数,获取当前时间作为对象
239 // Date 日期计算起始时间(零点)是从 1970年1月1日 00:00:00
240 // 1. 作为普通函数使用,返回当前时间字符串
241 console.log(Date())//Fri Aug 19 2022 15:16:22 GMT+0800 (中国标准时间)
242 
243 
244 //2.作为构造函数使用,返回日期对象
245 var date = new Date()
246 console.log(date);//Fri Aug 19 2022 15:16:22 GMT+0800 (中国标准时间)
247 
248 
249 //3.Data.now()返回当前时间距离零点时间的毫秒数(时间戳)
250 console.log(Date.now());//1660893471563
251 
252 
253 //4.Date.parse()解析日期字符串,返回指定日期距离零点的毫秒数
254 console.log(Date.parse('Fri Aug 19 2022 15:16:22 GMT+0800 (中国标准时间'));//1660893382000
255 console.log(Date.parse('Fri Aug 19 2022'));//1660838400000
256 console.log(Date.parse('2022-8-19 15:21:00'));//1660893660000
257 console.log(Date.parse('xxxxx'));
258 
259 
260 //get()方法获取具体的年月日,时分秒
261 var date2 = new Date()
262 var YY = date2.getFullYear();//
263 var MM = date2.getMonth() + 1;//
264 var dd = date2.getDate();//
265 var week = date2.getDay()//星期数
266 var hh = date2.getHours()//
267 var mm = date2.getMinutes()//
268 var ss = date2.getSeconds()//
269 var time = date2.getTime()//获取距离零点的毫秒数(时间戳)
270 console.log(YY,MM,dd,week,hh,mm,ss,time);//2022 8 19 5 15 31 11 1660894271618
271 
272 var date2 = new Date('2022-8-19 15:21:00')//可以传入时间戳,日期字符串转为日期对象
273 
274 
275 //set方法可以设置日期,与get对应
276 date2.setFullYear('2033');
277 date2.setDate(12)
278 console.log(date2);//Fri Aug 12 2033 15:21:00 GMT+0800 (中国标准时间)
复制代码

 

posted @   YBYZ  阅读(59)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示