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 = 0;
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(3, 4); // 输出12;
myfun8("hi,", "你好");//输出hi,你好;
myfun8("hi", 5);//输入错啦.
时间: 2024-08-03 05:54:37

call和apply还有bind的相关文章

JS中的call、apply、bind方法

JS中的call.apply.bind方法 一.call()和apply()方法 1.方法定义call方法: 语法:call([thisObj[,arg1[, arg2[,   [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 说明: call 方法可以用来代替另一个对象调用一个方法.call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象. 如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj.

图解call、apply、bind的异同及各种实战应用演示

一.图解call.apply.bind的异同 JavaScript中函数可以通过3种方法改变自己的this指向,它们是call.apply.bind.它们3个非常相似,但是也有区别.下面表格可以很直观看出三者的不同 方法 是否直接执行函数 传入的参数 调用方式 call 是 (context,arg1,arg2,arg3...) 第二个参数之后都是实参 function.call(context,arg1,arg2,arg3...) apply 是 (context,[arg1,arg2,arg

JS中的控制函数调用:call(),apply()和bind()

所有的函数都具有call(),apply()和bind()方法.它们可以在执行方法的时候用一个值指向this,并改变面向对象的作用域. apply方法: 以下的两种表达式是等价的: func(arg1,arg2,arg3) func.apply(null,[arg1,arg2,arg3]) apply()往往用在一个函数需要以数组的是形式接受多个参数时使用. 使用Math.max()来获得一个数组中最大的元素: Math.max.apply(null,[17,33,21]); call方法: 表

JavaScript中的call、apply、bind方法的区别

在JS中,this的指向是动态变化的,很可能在写程序的过程中,无意中破坏掉this的指向,所以我们需要一种可以把this的含义固定的技术,于是就有了call,apply和bind这三个方法 apply:应用某一对象的一个方法,用另一个对象替换当前对象.call:调用一个对象的一个方法,以另一个对象替换当前对象. △它们的共同之处:都可以用来代替另一个对象调用一个方法,将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象. △它们的不同之处:①apply:最多只能有两个参数—

使用call、apply和bind解决js中烦人的this,事件绑定时的this和传参问题

1.什么是this 在JavaScript中this可以是全局对象.当前对象或者任意对象,这完全取决于函数的调用方式,this 绑定的对象即函数执行的上下文环境(context). 为了帮助理解,让我们来一起看一段代码: // 作为对象方法调用 var test = { a : 5, b : 6, sum : function () { return this.a + this.b; // 此处this = test } } alert(test.sum()); // 11 作为对象调用时thi

javascript---call,apply,bind

对于这三个函数,估计大家都还是很模糊,具体是用来干什么?简而言之,是用来对象冒充的. 首先这三个方法是每个函数都包含的非继承的的方法. 我来搬砖一下,此文引用 http://www.cnblogs.com/cosiray/p/4512969.html javscript中call,apply,bind的区别 在JS中,这三者都是用来改变函数的this对象的指向的,他们有什么样的区别呢.在说区别之前还是先总结一下三者的相似之处:1.都是用来改变函数的this对象的指向的.2.第一个参数都是this

理解JS中的call、apply、bind方法

理解JS中的call.apply.bind方法(*****************************************************************) 在JavaScript中,call.apply和bind是Function对象自带的三个方法,这三个方法的主要作用是改变函数中的this指向. call.apply.bind方法的共同点和区别:apply . call .bind 三者都是用来改变函数的this对象的指向的:apply . call .bind 三者

数组去重,call、apply、bind之间的区别,this用法总结

一.数组去重,直接写到Array原型链上. 1 //该方法只能去除相同的数字 不会去判断24和'24'是不同的 所有数字和字符串数字是相同是重复的 2 Array.prototype.redup=function(){ 3 var obj={}; 4 for(var i=0;i<this.length;i++){ 5 var val=this[i]; 6 if(obj[val]==this[i]){ //如果发现重复的 7 this[i]=this[this.length-1]; //那就把最后

*JS核心:call、apply和bind

1. var func=function(a,b,c){ console.log([a,b,c]); }; func.apply(null,[1,2,3]); //[1,2,3] func.call(null,1,2,3); //[1,2,3] func.bind(null,1,2,3); //无输出 func.bind(null,1,2,3)(); //[1,2,3] func.bind(null)(2,3,4) //[2,3,4] 解析: 当使用apply或call时,如果我们传入第一个参数

Javascript中call,apply,bind方法的详解与总结

在 javascript之 this 关键字详解 文章中,谈及了如下内容,做一个简单的回顾: 1.this对象的涵义就是指向当前对象中的属性和方法. 2.this指向的可变性.当在全局作用域时,this指向全局:当在某个对象中使用this时,this指向该对象:当把某个对象的方法赋值给另外一个对象时,this会指向后一个对象. 3.this的使用场合有:在全局环境中使用:在构造函数中使用,在对象的方法中使用. 4.this的使用注意点,最重要的一点就是要避免多层嵌套使用this对象. 对this