//给函数的prototype新增名字为name,函数体为fn的函数

    Function.prototype.method =function(name,fn){
this.prototype[name] = fn;//this
return this;
} //再扩展Array的方法,新方法的名字,函数体,形参,实参 if(!Array.prototype.forEach){
//Array.prototype.forEach = function(fn,thisObj){} //this.fns.forEach( function(el){el(o);} ) function(el){el(o)是函数实参 Array.method("forEach",function(fn,thisObj){//forRach是函数名字,后面是函数声明,fn,thisObj是声明里的形参
var scope = thisObj || window;
for (var i = 0; i < this.length; i++) {
fn.call(scope,this[i],i,this);
}
})
}
if(!Array.prototype.filter){
//this.fns.filter(function(el){if(el != xxx){return el;}})
Array.method("filter",function(fn,thisObj){
var scope = thisObj || window;
var a = [];
for (var i = 0; i < array.length; i++) {
if( !fn.call(scope,this[i],i,this) ){
continue;
}
a.push(this[i])
}
//..........................
return a;
})
}
window.DED = window.DED||{};//有就用自己,没有就用空对象
DED.util = DED.util || {}
//观察者
DED.util.Observer = function(){
this.fns = []
}
//扩展他
DED.util.Observer.prototype = {
//观察
subscribe : function(fn){
this.fns.push(fn);
},
//取消观察
unsubscribe : function(fn){
this.fns = this.fns.filter(function(el){
if(el != fn){
return el;
}
})
},
//循环执行函数被观察的数组
fire:function(o){
this.fns.forEach(function(el){
el(o);
})
}
}
addEvent(items, 'click', function(e) {
var e = e || window.event;
var src = e.target || e.srcElement;
try {
e.preventDefault();
}
catch (ex) {
e.returnValue = false;
}
function F(){}
var f = function(){} Function.prototype.method =function(name,fn){
alert(1);
}
f.method();//
F.method();// function F(){}
var f = function(){} Function.prototype.method =function(name,fn){
this.prototype[name] = fn;
return this;
}
f.method("a",function(){alert("a");});//
F.method("b",function(){alert("b");});// //f.a();//f.a is not a function
f.prototype.a();//a
//F.b();//F.b is not a function
F.prototype.b()//b
new F().b();//b

形参看成构造函数传入的成员变量的值。函数名看成类名。this.看成成员属性和成员方法。

    function addCar(){
this.name = 11;
this.begin = function(){//对象的成员方法属性可以用中括号获取
alert(1);
}
}
new addCar().begin();//
alert(new addCar()["begin"]);//this.begin = function(){alert(1);}
alert(typeof new addCar()["begin"]);//function
new addCar()["begin"]();//
alert(new addCar()["name"]);// var rr = new addCar();
rr["age"] = 444;
alert(rr["age"]);//
rr["fff"] = function(){alert(777);}
rr["fff"]();//

js37---Function.prototype的更多相关文章

  1. Function.prototype.toString 的使用技巧

    Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...

  2. Object.prototype和Function.prototype一些常用方法

    Object.prototype 方法: hasOwnProperty 概念:用来判断一个对象中的某一个属性是否是自己提供的(主要是判断属性是原型继承还是自己提供的) 语法:对象.hasOwnProp ...

  3. Object.prototype 与 Function.prototype 与 instanceof 运算符

    方法: hasOwnProperty isPrototypeOf propertyIsEnumerable hasOwnProperty 该方法用来判断一个对象中的某一个属性是否是自己提供的( 住要用 ...

  4. JS魔法堂:再次认识Function.prototype.call

    一.前言                                大家先预计一下以下四个函数调用的结果吧! var test = function(){ console.log('hello w ...

  5. 一起Polyfill系列:Function.prototype.bind的四个阶段

    昨天边参考es5-shim边自己实现Function.prototype.bind,发现有不少以前忽视了的地方,这里就作为一个小总结吧. 一.Function.prototype.bind的作用 其实 ...

  6. Function.prototype.bind接口浅析

    本文大部分内容翻译自 MDN内容, 翻译内容经过自己的理解. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo ...

  7. JavaScript 函数绑定 Function.prototype.bind

    ECMAScript Edition5 IE9+支持原生,作用为将一个对象的方法绑定到另一个对象上执行. Function.prototype.bind = Function.prototype.bi ...

  8. Function.prototype.bind

    解析Function.prototype.bind 简介 对于一个给定的函数,创造一个绑定对象的新函数,这个函数和之前的函数功能一样,this值是它的第一个参数,其它参数,作为新的函数的给定参数. b ...

  9. 解析Function.prototype.bind

    简介 对于一个给定的函数,创造一个绑定对象的新函数,这个函数和之前的函数功能一样,this值是它的第一个参数,其它参数,作为新的函数的给定参数. bind的作用 bind最直接的作用就是改变this的 ...

  10. Function.prototype.call.apply结合用法

     昨天在网上看到一个很有意思的js面试题,就跟同事讨论了下,发现刚开始很绕最后豁然开朗,明白过来之后发现还是挺简单的,跟大家分享下!  题目如下: var a = Function.prototype ...

随机推荐

  1. [JLOI2011]飞行路线(分层图)

    [JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在 n 个城市设有业务,设这些城市分别标记为 0 到 n−1 ,一共有 m ...

  2. /*+parallel(t,4)*/在SQL调优中的重要作用!

    谈谈HINT /*+parallel(t,4)*/在SQL调优中的重要作用! /*+parallel(t,4)*/在大表查询等操作中能够起到良好的效果,基于并行查询要启动并行进程.分配任务与系统资源. ...

  3. EF框架—Database-First

    ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案,现已经包含在 Visual Studio 2008 S ...

  4. Bootstrapbutton组

    button组同意多个button被堆叠在同一行上.当你想要把button对齐在一起时,这就显得很实用. 基本button组 给div加上class .btn-group <!DOCTYPE h ...

  5. modSecurity规则学习(四)——规则指令编写

    规则语言是使用9个指令实现: 语法:SecRule VARIABLES OPERATOR [TRANSFORMATION_FUNCTIONS, ACTIONS] Variables 以下几种: Reg ...

  6. Install the IIS 6.0 Management Compatibility Components in Windows 7 or in Windows Vista from Control Panel

    https://technet.microsoft.com/en-us/library/bb397374(v=exchg.80).aspx Install the IIS 6.0 Management ...

  7. Mybatis传多个参数(推荐)

    Dao层的函数方法 int deleteMsgById(@Param("name") String name,@Param("id") String id); ...

  8. SQL 小技能

    1. SET STATISTICS TIMEON   附带原文 2.关于索引,是自己可以用Tsql语句建,也可以在设计表的时候选中某一个字段建立索引   一般而言,主键属于聚合索引(字典:A-Z);反 ...

  9. 威联通NAS 网站无法登录,可以ssh情况下重启设备方法

    步骤: 1.VPN登录NAS 2.PUTTY SSH登录设备 3.reboot设备 等待重启约5分钟.

  10. 【Docker自定制镜像之Dockerfile】

    镜像的定制,就是定制每一层所添加的配置.文件,如果可以把每一层修改.安装.构建.操作的命令都写入到一个脚本中,用脚本来构建.定制镜像,这个脚本就是Dockerfile Dockerfile是一个文本文 ...