- 浏览: 753288 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (981)
- javascript (102)
- java (212)
- JQuery (81)
- 幽默笑话 (2)
- 只言片语 (6)
- 开发小记 (283)
- Hibernate (17)
- DWR (4)
- SQL (10)
- spring-ibatis (8)
- linux (24)
- Struts1 (8)
- Struts2 (16)
- spring (42)
- Mybatis (16)
- css (18)
- servlet (8)
- jdbc (4)
- jsp (1)
- spring-jdbc (2)
- FreeMarker (2)
- MySQL (24)
- JQuery-Grid (7)
- jstl (5)
- 正则表达式 (1)
- 面试集锦 (86)
- unix (18)
- 开发工具 (23)
- ajax (5)
- webservice (4)
- log4j (3)
- oracle (15)
- extjs (14)
- 其他 (9)
- 优秀技术参考地址 (1)
- 性能 (3)
- 数据库 (25)
- 算法 (15)
- 设计模式 (10)
- Python (3)
- AIX (5)
- weblogic (1)
- shell (14)
- quartz (5)
- 图形文件FusionCharts (1)
- Websphere (4)
- 转载 (5)
- hadoop (1)
- highchart (24)
- perl (22)
- DB2 (7)
- JBoss (1)
- JQuery Easy UI (9)
- SpringMVC (5)
- ant (5)
- echart (9)
- log4J配置 (3)
- 多线程 (10)
- 系统架构 (7)
- nginx (3)
- loadrunner1 (1)
- 分布式 (1)
- Dubbo (1)
- Redis (2)
- JMS (4)
- 自动化测试 (3)
- Spring循环依赖的三种方式 (1)
- spring-boot (2)
- 高级测试 (9)
- github (2)
- sonar (1)
- docker (6)
- web前端性能优化 (1)
- spring-aop (6)
- rabbit (1)
- ELK (1)
- maven (1)
- minio (1)
最新评论
-
zengshaotao:
jstl1point0 写道很好啊,如果有带Session会话 ...
Nginx+Tomcat搭建高性能负载均衡集群 -
jstl1point0:
很好啊,如果有带Session会话的怎么搞呢
Nginx+Tomcat搭建高性能负载均衡集群
一,js中对象继承
js中有三种继承方式
1.js原型(prototype)实现继承
复制代码 代码如下:
<SPAN style="BACKGROUND-COLOR: #ffffff"><SPAN style="FONT-SIZE: 18px"><html>
<body>
<script type="text/javascript">
function Person(name,age){
this.name=name;
this.age=age;
}
Person.prototype.sayHello=function(){
alert("使用原型得到Name:"+this.name);
}
var per=new Person("马小倩",21);
per.sayHello(); //输出:使用原型得到Name:马小倩
function Student(){}
Student.prototype=new Person("洪如彤",21);
var stu=new Student();
Student.prototype.grade=5;
Student.prototype.intr=function(){
alert(this.grade);
}
stu.sayHello();//输出:使用原型得到Name:洪如彤
stu.intr();//输出:5
</script>
</body>
</html></SPAN></SPAN>
2.构造函数实现继承
复制代码 代码如下:
<SPAN style="FONT-SIZE: 18px"><html>
<body>
<script type="text/javascript">
function Parent(name){
this.name=name;
this.sayParent=function(){
alert("Parent:"+this.name);
}
}
function Child(name,age){
this.tempMethod=Parent;
this.tempMethod(name);
this.age=age;
this.sayChild=function(){
alert("Child:"+this.name+"age:"+this.age);
}
}
var parent=new Parent("江剑臣");
parent.sayParent(); //输出:“Parent:江剑臣”
var child=new Child("李鸣",24); //输出:“Child:李鸣 age:24”
child.sayChild();
</script>
</body>
</html></SPAN>
3.call , apply实现继承
复制代码 代码如下:
<SPAN style="FONT-SIZE: 18px"><html>
<body>
<script type="text/javascript">
function Person(name,age,love){
this.name=name;
this.age=age;
this.love=love;
this.say=function say(){
alert("姓名:"+name);
}
}
//call方式
function student(name,age){
Person.call(this,name,age);
}
//apply方式
function teacher(name,love){
Person.apply(this,[name,love]);
//Person.apply(this,arguments); //跟上句一样的效果,arguments
}
//call与aplly的异同:
//1,第一个参数this都一样,指当前对象
//2,第二个参数不一样:call的是一个个的参数列表;apply的是一个数组(arguments也可以)
var per=new Person("武凤楼",25,"魏荧屏"); //输出:“武凤楼”
per.say();
var stu=new student("曹玉",18);//输出:“曹玉”
stu.say();
var tea=new teacher("秦杰",16);//输出:“秦杰”
tea.say();
</script>
</body>
</html></SPAN>
二、call和apply的用法(详细介绍)
js中call和apply都可以实现继承,唯一的一点参数不同,func.call(func1,var1,var2,var3)对应的apply写法为:func.apply(func1,[var1,var2,var3])。
JS手册中对call的解释:
复制代码 代码如下:
<SPAN style="FONT-SIZE: 18px">call 方法
调用一个对象的一个方法,以另一个对象替换当前对象。
call([thisObj[,arg1[, arg2[, [,.argN]]]]])
参数
thisObj
可选项。将被用作当前对象的对象。
arg1, arg2, , argN
可选项。将被传递方法参数序列。
说明
call 方法可以用来代替另一个对象调用一个方法。call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。
如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。</SPAN>
说简单一点,这两函数的作用其实就是更改对象的内部指针,即改变对象的this指向的内容。这在面向对象的js编程过程中有时是很有用的。下面以apply为例,说说这两个函数在 js中的重要作用。如:
复制代码 代码如下:
<SPAN style="FONT-SIZE: 18px"> function Person(name,age){ //定义一个类
this.name=name; //名字
this.age=age; //年龄
this.sayhello=function(){alert(this.name)};
}
function Print(){ //显示类的属性
this.funcName="Print";
this.show=function(){
var msg=[];
for(var key in this){
if(typeof(this[key])!="function"){
msg.push([key,":",this[key]].join(""));
}
}
alert(msg.join(" "));
};
}
function Student(name,age,grade,school){ //学生类
Person.apply(this,arguments);//比call优越的地方
Print.apply(this,arguments);
this.grade=grade; //年级
this.school=school; //学校
}
var p1=new Person("卜开化",80);
p1.sayhello();
var s1=new Student("白云飞",40,9,"岳麓书院");
s1.show();
s1.sayhello();
alert(s1.funcName);</SPAN>
另外,Function.apply()在提升程序性能方面有着突出的作用:
我们先从Math.max()函数说起,Math.max后面可以接任意个参数,最后返回所有参数中的最大值。
比如
复制代码 代码如下:
<SPAN style="FONT-SIZE: 18px">alert(Math.max(5,8)); //8
alert(Math.max(5,7,9,3,1,6)); //9
//但是在很多情况下,我们需要找出数组中最大的元素。
var arr=[5,7,9,1];
//alert(Math.max(arr)); // 这样却是不行的。NaN
//要这样写
function getMax(arr){
var arrLen=arr.length;
for(var i=0,ret=arr[0];i<arrLen;i++){
ret=Math.max(ret,arr[i]);
}
return ret;
}
alert(getMax(arr)); //9
//换用apply,可以这样写
function getMax2(arr){
return Math.max.apply(null,arr);
}
alert(getMax2(arr)); //9
//两段代码达到了同样的目的,但是getMax2却优雅,高效,简洁得多。
//再比如数组的push方法。
var arr1=[1,3,4];
var arr2=[3,4,5];
//如果我们要把 arr2展开,然后一个一个追加到arr1中去,最后让arr1=[1,3,4,3,4,5]
//arr1.push(arr2)显然是不行的。 因为这样做会得到[1,3,4,[3,4,5]]
//我们只能用一个循环去一个一个的push(当然也可以用arr1.concat(arr2),但是concat方法并不改变arr1本身)
var arrLen=arr2.length;
for(var i=0;i<arrLen;i++){
arr1.push(arr2[i]);
}
//自从有了Apply,事情就变得如此简单
Array.prototype.push.apply(arr1,arr2); //现在arr1就是想要的结果</SPAN>
js中有三种继承方式
1.js原型(prototype)实现继承
复制代码 代码如下:
<SPAN style="BACKGROUND-COLOR: #ffffff"><SPAN style="FONT-SIZE: 18px"><html>
<body>
<script type="text/javascript">
function Person(name,age){
this.name=name;
this.age=age;
}
Person.prototype.sayHello=function(){
alert("使用原型得到Name:"+this.name);
}
var per=new Person("马小倩",21);
per.sayHello(); //输出:使用原型得到Name:马小倩
function Student(){}
Student.prototype=new Person("洪如彤",21);
var stu=new Student();
Student.prototype.grade=5;
Student.prototype.intr=function(){
alert(this.grade);
}
stu.sayHello();//输出:使用原型得到Name:洪如彤
stu.intr();//输出:5
</script>
</body>
</html></SPAN></SPAN>
2.构造函数实现继承
复制代码 代码如下:
<SPAN style="FONT-SIZE: 18px"><html>
<body>
<script type="text/javascript">
function Parent(name){
this.name=name;
this.sayParent=function(){
alert("Parent:"+this.name);
}
}
function Child(name,age){
this.tempMethod=Parent;
this.tempMethod(name);
this.age=age;
this.sayChild=function(){
alert("Child:"+this.name+"age:"+this.age);
}
}
var parent=new Parent("江剑臣");
parent.sayParent(); //输出:“Parent:江剑臣”
var child=new Child("李鸣",24); //输出:“Child:李鸣 age:24”
child.sayChild();
</script>
</body>
</html></SPAN>
3.call , apply实现继承
复制代码 代码如下:
<SPAN style="FONT-SIZE: 18px"><html>
<body>
<script type="text/javascript">
function Person(name,age,love){
this.name=name;
this.age=age;
this.love=love;
this.say=function say(){
alert("姓名:"+name);
}
}
//call方式
function student(name,age){
Person.call(this,name,age);
}
//apply方式
function teacher(name,love){
Person.apply(this,[name,love]);
//Person.apply(this,arguments); //跟上句一样的效果,arguments
}
//call与aplly的异同:
//1,第一个参数this都一样,指当前对象
//2,第二个参数不一样:call的是一个个的参数列表;apply的是一个数组(arguments也可以)
var per=new Person("武凤楼",25,"魏荧屏"); //输出:“武凤楼”
per.say();
var stu=new student("曹玉",18);//输出:“曹玉”
stu.say();
var tea=new teacher("秦杰",16);//输出:“秦杰”
tea.say();
</script>
</body>
</html></SPAN>
二、call和apply的用法(详细介绍)
js中call和apply都可以实现继承,唯一的一点参数不同,func.call(func1,var1,var2,var3)对应的apply写法为:func.apply(func1,[var1,var2,var3])。
JS手册中对call的解释:
复制代码 代码如下:
<SPAN style="FONT-SIZE: 18px">call 方法
调用一个对象的一个方法,以另一个对象替换当前对象。
call([thisObj[,arg1[, arg2[, [,.argN]]]]])
参数
thisObj
可选项。将被用作当前对象的对象。
arg1, arg2, , argN
可选项。将被传递方法参数序列。
说明
call 方法可以用来代替另一个对象调用一个方法。call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。
如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。</SPAN>
说简单一点,这两函数的作用其实就是更改对象的内部指针,即改变对象的this指向的内容。这在面向对象的js编程过程中有时是很有用的。下面以apply为例,说说这两个函数在 js中的重要作用。如:
复制代码 代码如下:
<SPAN style="FONT-SIZE: 18px"> function Person(name,age){ //定义一个类
this.name=name; //名字
this.age=age; //年龄
this.sayhello=function(){alert(this.name)};
}
function Print(){ //显示类的属性
this.funcName="Print";
this.show=function(){
var msg=[];
for(var key in this){
if(typeof(this[key])!="function"){
msg.push([key,":",this[key]].join(""));
}
}
alert(msg.join(" "));
};
}
function Student(name,age,grade,school){ //学生类
Person.apply(this,arguments);//比call优越的地方
Print.apply(this,arguments);
this.grade=grade; //年级
this.school=school; //学校
}
var p1=new Person("卜开化",80);
p1.sayhello();
var s1=new Student("白云飞",40,9,"岳麓书院");
s1.show();
s1.sayhello();
alert(s1.funcName);</SPAN>
另外,Function.apply()在提升程序性能方面有着突出的作用:
我们先从Math.max()函数说起,Math.max后面可以接任意个参数,最后返回所有参数中的最大值。
比如
复制代码 代码如下:
<SPAN style="FONT-SIZE: 18px">alert(Math.max(5,8)); //8
alert(Math.max(5,7,9,3,1,6)); //9
//但是在很多情况下,我们需要找出数组中最大的元素。
var arr=[5,7,9,1];
//alert(Math.max(arr)); // 这样却是不行的。NaN
//要这样写
function getMax(arr){
var arrLen=arr.length;
for(var i=0,ret=arr[0];i<arrLen;i++){
ret=Math.max(ret,arr[i]);
}
return ret;
}
alert(getMax(arr)); //9
//换用apply,可以这样写
function getMax2(arr){
return Math.max.apply(null,arr);
}
alert(getMax2(arr)); //9
//两段代码达到了同样的目的,但是getMax2却优雅,高效,简洁得多。
//再比如数组的push方法。
var arr1=[1,3,4];
var arr2=[3,4,5];
//如果我们要把 arr2展开,然后一个一个追加到arr1中去,最后让arr1=[1,3,4,3,4,5]
//arr1.push(arr2)显然是不行的。 因为这样做会得到[1,3,4,[3,4,5]]
//我们只能用一个循环去一个一个的push(当然也可以用arr1.concat(arr2),但是concat方法并不改变arr1本身)
var arrLen=arr2.length;
for(var i=0;i<arrLen;i++){
arr1.push(arr2[i]);
}
//自从有了Apply,事情就变得如此简单
Array.prototype.push.apply(arr1,arr2); //现在arr1就是想要的结果</SPAN>
发表评论
-
javascript与日期的相关操作
2018-01-08 17:02 300Js获取当前日期时间及其它操作 var myDate ... -
面向对象
2017-08-24 17:31 447var objPro = { name:"zst ... -
两次encodeURI和URLDecode的原理分析
2017-08-24 12:15 3959当使用地址栏提交查询参数时,如果不编码,非英文字符会按照操 ... -
encodeURI
2017-08-14 21:29 480对于ajax请求,经常会在地址栏传入一些参数,涉及的中文的, ... -
encodeURL
2017-05-04 18:10 492.encodeURL函数主要是来对URI来做转码,它默认是采 ... -
cookie操作 JQuery
2017-03-23 22:18 480首先包含jQuery的库文件,在后面包含 jquery.coo ... -
表单参数一并提交
2017-03-23 13:09 531对于一般的table数据,并且还带有查询条件,我们都会使用j ... -
功能参考
2016-09-12 17:48 427<%@ page language="jav ... -
树形节点设权重,叶子节点设权重
2016-08-19 12:01 1040<%@ page language="jav ... -
除去缓存
2016-08-11 17:04 347IE的showModalDialog方法,缓存很是头疼 ... -
event.srcElement
2016-07-22 16:51 481因为页面的显示内容需要动态生成,所以难免有一些字符串的拼接, ... -
三重循环部分
2016-07-01 11:44 459一串json数据data,格式形如: {{WDNAME:A ... -
base href, new Highchart.Chart()
2016-05-27 17:35 526<% String path = request.g ... -
鼠标点击的位置,滚动条,兼容
2016-05-19 15:58 575j经常要计算鼠标当前点击的位置,并且页面经常发生了滚动。这个 ... -
表单提交页面不跳转
2016-04-05 19:38 935页面发生表单不跳转,我们可以ajax的形式 当然,有 ... -
HTML图片热区map area的用法
2016-01-04 14:56 519HTML图片热区map area的用法 <a ... -
获取页面高度
2015-12-15 10:26 424$(document).height() //是获取整个页 ... -
iframe中子父窗口互调的js方法
2015-11-12 18:12 909一、父窗口调用iframe子窗口方法 1、HTML语法:&l ... -
数组去重
2015-10-30 08:38 506//将一个含有相同值的数组去重 function getD ... -
dtree树构建
2015-09-15 15:19 432dTree 主页:http://destroydrop.co ...
相关推荐
JavaScript中call与apply方法
js的继承方法小结(prototype、call、apply)(推荐).docx
js中call与apply的用法小结.docx
js的原型继承 — prototype 先说下什么是prorotype? js中,俗话说“一切皆对象”。用new 出来的都是函数对象;否则就是普通对象 函数对象都有prototype(原型对象);而普通对象则只有__proto__(原型指针) ...
在 javascript 中,call 和 apply 都是为了改变某个函数运行时的上下文(context)而存在的,一般来说,this总是指向调用某个方法的对象,但是使用call()和apply()方法时,就会改变this的指向 语法: apply() 接收两...
淡淡简单描述javascript中方法apply和call
本文介绍了js继承当中的call和apply方法,从定义到常用实例都做了分析,非常的实用,推荐给小伙伴们。
js中apply与call简单用法详解.docx
apply的其他巧妙用法(一般在什么情况下可以使用apply) 我首先从网上查到关于apply和call的定义,然后用示例来解释这两个方法的意思和如何去用. apply:方法能劫持另外一个对象的方法,继承另外一个对象的属性. ...
详解js中的apply与call的用法.docx
1、call,apply都属于Function.prototype的一个方法,它是JavaScript引擎内在实现的,因为属于Function.prototype,所以每个Function对象实例(就是每个方法)都有call,apply属性。既然作为方法的属性,那它们的使用...
理解JavaScript的caller callee call apply
本文实例讲述了javascript基于原型链的继承及call和apply函数用法。分享给大家供大家参考,具体如下: 1. 继承是面向对象编程语言的一个重要特性,比如Java中,通过extend可以实现多继承,但是JavaScript中的继承...
js中arguments,caller,callee,apply的用法小结.docx
call, apply都属于Function.prototype的一个方法,它是JavaScript引擎内在实现的,因为属于Function.prototype,所以每个Function对象实例,也就是每个方法都有call, apply属性.既然作为方法的属性,那它们的使用就当然是...
javascript中apply、call和bind的用法区分_.docx
NULL 博文链接:https://ufoqhmdt.iteye.com/blog/1676748