JS构造函数的用法和JS原型
$(function(){
//
var rec = new Rectangle(5, 10);
//alert(rec.width + "*" + rec.height + "=" + rec.area());
//alert(rec.hasOwnProperty("width"));
//alert("area" in rec);
//alert(rec.toString());
//
var message = "hello world";
//alert(message.endsWith("d"));
//alert(Array.prototype.push);
//表示的最大数字和最小数字
//alert(Number.MAX_VALUE + " OR " + Number.MIN_VALUE);
//
var n = 17;
//alert(n.toString(2)); //其中2代表转为2进制,若将其改为16,则输出的是16进制的值s
//
//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);
}
我只会编码,因为我相信代码比任何的语言表达更能让人信服!!!
原创来自:背着理想去流量
JS构造函数的用法和JS原型的更多相关文章
- JS构造函数、对象工厂、原型模式
1.对象创建的3中方法 1.1.对象字面量 var obj = { name: "mingzi", work: function () { console.log("wo ...
- 深入研究js构造函数和原型
很快就要从新浪离职了,最近心情比较轻松,抽点空整理一下构造函数和原型的机理. 我们都知道,在经典设计模式中我们最常用的就是工厂模式.构造函数模式.原型模式这几种,听起来‘模式’好像很高大上的样子,实际 ...
- js最好的继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。
js最好的继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法. function ClassA(sColor) { this.color = sColor; } Class ...
- js一种继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。
js一种继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法. function ClassA(sColor) { this.color = sColor; } ClassA ...
- js高级——构造函数,实例对象和原型对象——prototype、__proto__和constructor构造器
一.前言 了解JavaScript面向对象,需要先了解三个名词: 构造函数,实例对象和原型对象. 注意:JavaScript中没有类(class)的概念,取而代之的是构造函数,两者类似却又有很大的差别 ...
- js 构造函数 & 静态方法 & 原型 & 实例方法
js 构造函数 & 静态方法 & 原型 & 实例方法 ES5 "use strict"; /** * * @author xgqfrms * @licens ...
- JS高级---构造函数,实例对象和原型对象,三者关系
构造函数,实例对象和原型对象,三者关系 构造函数里面有原型(prototype)属性,即原型对象 原型对象里的constryctor构造器指向构造函数 通过构造函数,实例化,创建的就是实例对象. 实例 ...
- 谈谈JS构造函数
//构造函数 //使自己的对象多次复制,同时实例根据设置的访问等级可以访问其内部的属性和方法 //当对象被实例化后,构造函数会立即执行它所包含的任何代码 function myObject(msg) ...
- Class和普通js构造函数的区别
Class 在语法上更加贴合面向对象的写法 Class 实现继承更加易读.易理解 更易于写 java 等后端语言的使用 本质还是语法糖,使用 prototype Class语法 typeof Math ...
随机推荐
- MFC 网络编程 -- 总结
原文链接:http://www.cnblogs.com/lidabo/archive/2012/07/19/2598734.html 1.基于 TCP 的 socket 编程 /* 服务器端程序流程: ...
- 问题解决——Win7 64 安装 AutoCAD 2010 32位 和 清华天河PC CAD
最近单位组了一台电脑,配置还好,E3大法+R9 280,装了Win7 64位系统. ========================================================== ...
- Eclipse编译去除svn文件夹
使用Eclipse编译文件后,classes文件中总是有.svn的文件夹,这些文件没有什么用,而且影响build的速度 "Project->Properties->Java Bu ...
- Azure 上为Liunx VM 挂载File类型的存储。
1. Create a storage account in Azure, copy the storage account endpoint URL (with postfix of "f ...
- Windows Live Writer离线编写博客
WLW最新版本为2012,官网下载 Windows Live Writer配置步骤 使用Windows Live Writer 2012和Office Word 2013 发布文章到博客园全面总结 L ...
- LAMP编译参数查看
Linux下查看Nginx.Napache.MySQL.PHP的编译参数的命令如下: 1.nginx编译参数:#/usr/local/nginx/sbin/nginx -V2.apache编译参数:# ...
- [转]excel set drop-down values based on vlookup
本文转自:http://stackoverflow.com/questions/10657811/set-drop-down-values-based-on-vlookup 问: I have a w ...
- leetcode_438_Find All Anagrams in a String_哈希表_java实现
题目: Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Stri ...
- python中怎么查看当前工作目录和更改工作目录
查询当前目录:os.getcwd() 更改当前目录:os.chdir()
- UESTC 887 方伯伯的儿童节 --树形DP
定义: 1.dp[u][1]表示u这个点设立糖果发放点且u这棵子树满足条件时的最少糖果点数 2.dp[u][0]表示u这个点不设立发放点且u这棵子树满足条件时的最少糖果点数 设v1,v2……vn为u的 ...