call和apply还有bind
有图有真相

function myfun1(){
//这是私有属性
var private1 = "这是私有属性1";
var privateMethod = function(){
alert(private1);
}
//这是实例属性
this.publicvar = "这是实例属性1";
this.public1 = function(){
privateMethod();
}
}
//-//--------------//-//---------------------------------
var newfun1 = new myfun1();
newfun1.public1(); //这是私有属性
alert(newfun1.publicvar);//这是实例属性
alert(newfun1.private1); // undefined newfun1.privateMethod(); //运行错误
//--//--//---///------------------------------------------
//--//--//---///-------------------执行1-------------------
//--//--//---///------------------------------------------
function myfun2(){
}
myfun2.staticvar = "这是静态属性2";
myfun2.staticmethod = function(){
alert(myfun2.staticvar);
}
//--//--//---///------------------------------------------
//--//--//---///-------------------执行2-------------------
//--//--//---///------------------------------------------
var newfun2 = new myfun2();
//newfun2.staticmethod();//运行错误;
alert(newfun2.staticvar);//undefined
//------//-------------//-------------------------------
//静态私有成员
var myfun3 = (function(){
function privateProperty(){
}
privateProperty.staticvar = "这是静态私有成员3";
privateProperty.staticmethod = function(){
alert(privateProperty.staticvar);
}
privateProperty.staticmethod();
return privateProperty
})();
alert(newfun3.staticvar);//这是静态私有成员3
//---//--------//-----------------//-------------
//静态类
var funcount = ;
var myfun4 = new function(){
funcount++;
this.printCount = function(){
alert(funcount);
}
}
myfun4.printCount(); //输出1;
myfun4.printCount(); //输出1;
myfun4.prototype.amethod = function(){
alert("原型对象4");
}//运行错误
var newfun4 = new myfun4();
newfun4.amethod();
//------------------//---------------------------------
//运行错误,说明myfun3创建并实例化之后就不能再为它添加方法,属性
new myfun3.constructor().printCount();//如果你确实想实例化,这样也可以.
//原型继承
var myfun5 = function(){
}
myfun5.prototype.myfun5_extend = function(){
alert("这是原型继承的5");
}
var myfun5_sub = function(){
}
myfun5_sub.prototype = new myfun5();
var newfun5 = new myfun5_sub();
newfun5.myfun5_extend(); //这是原型继承的
//调用继承
var myfun6 = function(){
this.method_p = function(){
alert("这是调用继承的6");
}
}
var myfun6_sub = function(){
myfun6.call(this);
}
var newfun6 = new myfun6_sub();
newfun6.method_p();//这是调用继承的
//覆盖
var myfun7 = function(){
this.method = function(){
alert("这是父对象方法7");
}
}
var myfun7_sub = function(){
this.method = function(){
alert("这是子对象方法8");
}
}
myfun7_sub.prototype = new myfun7();
var newfun7 = new myfun7_sub();
newfun7.method(); //这是子对象方法,父对象方法被覆盖了.
//多态
function myfun8(a, b){
var a = a;
var b = b;
if (typeof a == "number" && typeof b == "number") {
alert(a * b);
} else if (typeof a == "string" && typeof b == "string") {
alert(a + b);
} else {
alert("输入错啦");
}
}
myfun8(, ); // 输出12;
myfun8("hi,", "你好");//输出hi,你好;
myfun8("hi", );//输入错啦.
call和apply还有bind的更多相关文章
- JS核心系列:浅谈 call apply 与 bind
在JavaScript 中,call.apply 和 bind 是 Function 对象自带的三个方法,这三个方法的主要作用是改变函数中的 this 指向,从而可以达到`接花移木`的效果.本文将对这 ...
- Javascript中call,apply,bind方法的详解与总结
在 javascript之 this 关键字详解 文章中,谈及了如下内容,做一个简单的回顾: 1.this对象的涵义就是指向当前对象中的属性和方法. 2.this指向的可变性.当在全局作用域时,thi ...
- JS中call、apply、bind使用指南,带部分原理。
为什么需要这些?主要是因为this,来看看this干的好事. box.onclick = function(){ function fn(){ alert(this); } fn();}; 我们原本以 ...
- Javascript中call、apply、bind函数
javascript在函数创建的时候除了自己定义的参数外还会自动新增this和arguments两个参数 javascript中函数也是对象,call.apply.bind函数就是函数中的三个函数,这 ...
- 图解call、apply、bind的异同及各种实战应用演示
一.图解call.apply.bind的异同 JavaScript中函数可以通过3种方法改变自己的this指向,它们是call.apply.bind.它们3个非常相似,但是也有区别.下面表格可以很直观 ...
- js里function的apply vs. bind vs. call
js里除了直接调用obj.func()之外,还提供了另外3种调用方式:apply.bind.call,都在function的原型里.这3种方法的异同在stackoverflow的这个答案里说的最清楚, ...
- JS之apply,call,bind区别
为了加深对基础知识的理解,今天再复习下js中的apply,call,bind的区别和用法.整理笔记的过程也是一个再次学习的过程. apply和call js中的调用apply和call方法可以改变某个 ...
- JavaScript中call、apply、bind、slice的使用
1.参考资料 http://www.cnblogs.com/coco1s/p/4833199.html 2.归结如下 apply . call .bind 三者都是用来改变函数的this对象的指向 ...
- JS中的控制函数调用:call(),apply()和bind()
所有的函数都具有call(),apply()和bind()方法.它们可以在执行方法的时候用一个值指向this,并改变面向对象的作用域. apply方法: 以下的两种表达式是等价的: func(arg1 ...
- JS中的call、apply、bind方法
JS中的call.apply.bind方法 一.call()和apply()方法 1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[, [,.argN]]] ...
随机推荐
- SCADESuite嵌入式软件基于模型的开发
SCADE Suite®产品是针对高安全性嵌入式软件的基于模型的开发环境 SCADE Suite是高安全性嵌入式软件的开发标准,其应用领域涵盖航空.国防.轨道交通.能源和重工业.专为最高等级的质量和安 ...
- Ⅰ.AngularJS的点点滴滴--引导
AngularJS已经被很多人像炒冷饭一样炒过啦,大部分都是直接复制官方文档没有说明一些注意事项,不过什么都要从头开始吧 页面引导实例化 1.自动实例化 <html> <script ...
- oracle知识杂记
索引类型 1.B-树索引 分区索引.压缩索引.函数索引等都属于B-树索引 2.位图索引 3.索引组织表 索引管理 1.普通单列索引 对where条件.group ...
- php之常量小见
php设置常量有二种方法,一为define(),二为使用关键字const. define()函数带有三个形参,一为常量名,通常以大写字母命名,二为值,三为是否对大小写敏感,其值为可选,默认为false ...
- 【锋利的jQuery】学习笔记02
第二章 jQuery选择器 一.jQuery选择器的优势 写法简洁 $("div") 支持css2和css3选择器(对于css3选择器支持这一项,我认为应该是jQuery首先创造并 ...
- IIS 上发布ASP.NET5
原文 http://docs.asp.net/en/latest/publishing/iis.html 1.安装HTTP Platform Handler ,这个是必需的,不然无法通过IIS访问AS ...
- Canvas保存图片保存到本地
使用Canvas绘图,将图片保存到本地方法 一.使用HTML5 a标签的download属性,将图片保存到本地,不需要链接服务器 关于download属性:HTML5 <a>标签downl ...
- Quartz.net 定时任务在IIS中未按时执行
IIS 垃圾回收机制下解决Quartz.net 的不执行问题 IIS中涉及了垃圾回收机制,quartz.net 在ASP.NET 项目中可以实现线程监控定时执行任务,但是在IIS7.5机一下版本中涉及 ...
- insert一句话实现插入一条数据并且返回这条数据的某列
insert into [table] output inserted.columnName values();
- React学习笔记(一) 基础知识
现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了我. React的基 ...