JS构造函数的用法和JS原型
$(function(){ //1 var rec = new Rectangle(5, 10); //alert(rec.width + "*" + rec.height + "=" + rec.area()); //alert(rec.hasOwnProperty("width")); //alert("area" in rec); //alert(rec.toString()); //2 var message = "hello world"; //alert(message.endsWith("d")); //alert(Array.prototype.push); //表示的最大数字和最小数字 //alert(Number.MAX_VALUE + " OR " + Number.MIN_VALUE); //3 var n = 17; //alert(n.toString(2)); //其中2代表转为2进制,若将其改为16,则输出的是16进制的值s //4 //alert(parseInt("15",8)); //输出13,15的8进制(13 == 1*8+5*0) //5.1 //alert(fun(5)); //5.2 //alert(fun2(5)); var point = new Object(); point.x = 1; point.y = 2; //alert(point.x + " AND " + point.y); //alert(delete point.x); //这一行代码有无执行会影响到下一行代码的执行结果 //alert("x" in point); var arr = [1, 2, 3, 4, 5]; //alert(arr.concat(4, [5, [6, 7]])); //alert(arr.slice(1, 4)); //截取数据第2个到第5个元素,包括第2个不包括第5个 var str = "chinese"; //alert(str.lastEndChar()); //alert(location.href); //alert(location.search); //获取url地址?后面的参数列表 //打开新窗口 //window.open("http://www.baidu.com", "minWin", "width=400, height=350, status=yes, resizable=yes"); }); //JS构造函数 1 function Rectangle(w, h) { this.width = w; this.height = h; this.area = function () { //return this.width * this.height; //写法1.1 with (this) { //写法1.2 return width * height; } } } //设置Rectangle的原型值,若构造函数中无定义area方法,则默认使用原型 Rectangle.prototype.area = function () { return 5; } //重写Rectangle的toString方法 Rectangle.prototype.toString = function () { return this.width + " " + this.height ; } //2 查看字符串的结尾是否是以c结尾的 返回true OR false String.prototype.endsWith = function (c) { return (c == this.charAt(this.length - 1)); } //5.1 lambda写法(拉姆达) var fun = function (x) { return x + x; } //5.2 var fun2 = new Function("x", "return x+x"); //返回字符串末尾的指定个数的字符 String.prototype.lastEndChar = function (num) { num = (null != num)? num : 1; return this.substring(this.length - num, this.length); }
我只会编码,因为我相信代码比任何的语言表达更能让人信服!!!
原创来自:背着理想去流量
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?