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]]] ...
随机推荐
- 单表扫描,MySQL索引选择不正确 并 详细解析OPTIMIZER_TRACE格式
单表扫描,MySQL索引选择不正确 并 详细解析OPTIMIZER_TRACE格式 一 表结构如下: 万行 CREATE TABLE t_audit_operate_log ( Fid b ...
- Yet Another 10 Common Mistakes Java Developers Make When Writing SQL (You Won’t BELIEVE the Last One)--reference
(Sorry for that click-bait heading. Couldn’t resist ;-) ) We’re on a mission. To teach you SQL. But ...
- Sqlserver中实现oralce 数据库的rownumber
引用自:http://cai555.javaeye.com/blog/466033 方法1: with temp as ( select row_number() over(order by city ...
- Abstract Factory 抽象工厂模式
提供一个创建一些列相关或相互依赖对象的接口,而无需指定它们具体的类. 抽象工厂顾名思义就是对工厂的抽象,它提供了一组创建抽象产品对象的操作接口,我们实际使用的是抽象工厂的派生类,派生类中提供了操作的具 ...
- CI框架篇之模型篇--AR操作(2)
CodeIgniter 和众多的框架一样,有属于自己的一套对数据库的操作方式,本框架更是如此 有属于自己的一套对数据库的安全并且简单的操作, 成为AR操作:下面来对AR操作进行介绍: 首先,确定要启动 ...
- ASP.NET程序如何更新发布
ASP.NET程序如何更新发布 一.首先右键项目,点击“发布” 然后,新建名称.类型选择文件,然后点击下一步: 点击发布即可! 二.
- Python:文件操作
#!/usr/bin/python3 str1 = input("请输入:") print("你输入的是:",str1) f=open("abc.tx ...
- GDB源代码查找路径
在gdb程序的时候,有时候会发现源代码文件找不到,对于那些带调试信息的系统库或者第三方库,很多时候当你真正想gdb去追他源代码的时候你会发现gdb根本找不到这些源代码路径.这个时候有两种选择: [1] ...
- 原生js判断是否有某个class,如果有就删掉,没有加上
<style> #div1 { width: 100px; height: 100px; position: absolute; } .div1 { background: red; } ...
- 《深入.NET平台和C#编程》内部测试题-笔试试卷
1.以下关于序列化和反序列化的描述错误的是( C). a.序列化是将对象的状态存储到特定存储介质中的过程 b.二进制格式化器的Serialize()和Deserialize()方法可以用来实现序列化和 ...