有图有真相

 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的更多相关文章

  1. JS核心系列:浅谈 call apply 与 bind

    在JavaScript 中,call.apply 和 bind 是 Function 对象自带的三个方法,这三个方法的主要作用是改变函数中的 this 指向,从而可以达到`接花移木`的效果.本文将对这 ...

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

    在 javascript之 this 关键字详解 文章中,谈及了如下内容,做一个简单的回顾: 1.this对象的涵义就是指向当前对象中的属性和方法. 2.this指向的可变性.当在全局作用域时,thi ...

  3. JS中call、apply、bind使用指南,带部分原理。

    为什么需要这些?主要是因为this,来看看this干的好事. box.onclick = function(){ function fn(){ alert(this); } fn();}; 我们原本以 ...

  4. Javascript中call、apply、bind函数

    javascript在函数创建的时候除了自己定义的参数外还会自动新增this和arguments两个参数 javascript中函数也是对象,call.apply.bind函数就是函数中的三个函数,这 ...

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

    一.图解call.apply.bind的异同 JavaScript中函数可以通过3种方法改变自己的this指向,它们是call.apply.bind.它们3个非常相似,但是也有区别.下面表格可以很直观 ...

  6. js里function的apply vs. bind vs. call

    js里除了直接调用obj.func()之外,还提供了另外3种调用方式:apply.bind.call,都在function的原型里.这3种方法的异同在stackoverflow的这个答案里说的最清楚, ...

  7. JS之apply,call,bind区别

    为了加深对基础知识的理解,今天再复习下js中的apply,call,bind的区别和用法.整理笔记的过程也是一个再次学习的过程. apply和call js中的调用apply和call方法可以改变某个 ...

  8. JavaScript中call、apply、bind、slice的使用

    1.参考资料 http://www.cnblogs.com/coco1s/p/4833199.html   2.归结如下 apply . call .bind 三者都是用来改变函数的this对象的指向 ...

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

    所有的函数都具有call(),apply()和bind()方法.它们可以在执行方法的时候用一个值指向this,并改变面向对象的作用域. apply方法: 以下的两种表达式是等价的: func(arg1 ...

  10. JS中的call、apply、bind方法

    JS中的call.apply.bind方法 一.call()和apply()方法 1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[,   [,.argN]]] ...

随机推荐

  1. careercup-数组和字符串1.3

    1.3 给定两个字符串,请编写程序,确定其中一个字符串的字符重新排序后,能否变成另一个字符串. C++实现代码: #include<iostream> #include<map> ...

  2. JAVA格式化时间日期

    JAVA格式化时间日期 import java.util.Date; import java.text.DateFormat; /** * 格式化时间类 * DateFormat.FULL = 0 * ...

  3. [转] React Router 使用教程

    PS:react-route就是一个决定生成什么父子关系的组件,一般和layout结合起来,保证layout不行,内部的子html进行跳转 你会发现,它不是一个库,也不是一个框架,而是一个庞大的体系. ...

  4. Motion——shake攻略

    1.子类化窗口 如果响应链中没有motionEnded:withEvent:消息的接收者,那么该消息就会被发送给应用程序的window对象.所以需要在window对象上拦截motionEnded:wi ...

  5. 学习java随笔第六篇:数组

    一维数组 创建一维数组并输出 public class OneDimensionalArray { public static void main(String argas[]) { int i[]= ...

  6. Web.xml配置详解之context-param (加载spring的xml,然后初始化bean看的)

    http://www.cnblogs.com/goody9807/p/4227296.html(很不错啊) 容器先加载spring的xml,然后初始化bean时,会为bean赋值,包括里面的占位符

  7. CI 笔记7,easyui 异步加载

    在做后台导航时,需要异步加载,pid和id的循环问题,在controller中,建立另外一个方法,嵌套循环,查找是否pid〉1. public function nav_list() { $this- ...

  8. 一行代码实现headView弹簧拉伸效果

    前言 很多app的个人中心上部的headView都实现了弹簧拉伸的效果,即tableView的top并不随着下拉而滑动,而是紧紧的停在屏幕的最上方. 我们今天就分析一下这个效果的实现方式. 分析 关键 ...

  9. WPF常用控件应用demo

    WPF常用控件应用demo 一.Demo 1.Demo截图如下: 2.demo实现过程 总体布局:因放大缩小窗体,控件很根据空间是否足够改变布局,故用WrapPanel布局. <ScrollVi ...

  10. html5与js关于input[type='text']文本框value改变触发事件一些属性的区别oninput,onpropertychange,onchange和文本框的value点击全选状态onclick="select();"。做购物车页面时会要用到。

    关于input[type='text']文本框value改变触发事件一些属性的区别oninput,onpropertychange,onchange和文本框的点击全选状态onclick="s ...