js37---Function.prototype
//给函数的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的更多相关文章
- Function.prototype.toString 的使用技巧
Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...
- Object.prototype和Function.prototype一些常用方法
Object.prototype 方法: hasOwnProperty 概念:用来判断一个对象中的某一个属性是否是自己提供的(主要是判断属性是原型继承还是自己提供的) 语法:对象.hasOwnProp ...
- Object.prototype 与 Function.prototype 与 instanceof 运算符
方法: hasOwnProperty isPrototypeOf propertyIsEnumerable hasOwnProperty 该方法用来判断一个对象中的某一个属性是否是自己提供的( 住要用 ...
- JS魔法堂:再次认识Function.prototype.call
一.前言 大家先预计一下以下四个函数调用的结果吧! var test = function(){ console.log('hello w ...
- 一起Polyfill系列:Function.prototype.bind的四个阶段
昨天边参考es5-shim边自己实现Function.prototype.bind,发现有不少以前忽视了的地方,这里就作为一个小总结吧. 一.Function.prototype.bind的作用 其实 ...
- Function.prototype.bind接口浅析
本文大部分内容翻译自 MDN内容, 翻译内容经过自己的理解. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo ...
- JavaScript 函数绑定 Function.prototype.bind
ECMAScript Edition5 IE9+支持原生,作用为将一个对象的方法绑定到另一个对象上执行. Function.prototype.bind = Function.prototype.bi ...
- Function.prototype.bind
解析Function.prototype.bind 简介 对于一个给定的函数,创造一个绑定对象的新函数,这个函数和之前的函数功能一样,this值是它的第一个参数,其它参数,作为新的函数的给定参数. b ...
- 解析Function.prototype.bind
简介 对于一个给定的函数,创造一个绑定对象的新函数,这个函数和之前的函数功能一样,this值是它的第一个参数,其它参数,作为新的函数的给定参数. bind的作用 bind最直接的作用就是改变this的 ...
- Function.prototype.call.apply结合用法
昨天在网上看到一个很有意思的js面试题,就跟同事讨论了下,发现刚开始很绕最后豁然开朗,明白过来之后发现还是挺简单的,跟大家分享下! 题目如下: var a = Function.prototype ...
随机推荐
- iOS日期转换之UTC/GMT时间格式
GMT只需要将代码中的UTC替换为GMT即可 //将本地日期字符串转为UTC日期字符串 //本地日期格式:2013-08-03 12:53:51 //可自行指定输入输出格式 -(NSString *) ...
- 洛谷 P1824 进击的奶牛
P1824 进击的奶牛 题目描述 Farmer John建造了一个有N(2<=N<=100,000)个隔间的牛棚,这些隔间分布在一条直线上,坐标是x1,...,xN (0<=xi&l ...
- 百度地图SDK for Android【Demo兴趣点搜索】
百度地图SDK为开发人员提供了便捷的检索服务. 今天我将为大家介绍Poi检索相关的内容. 首先,我们要构建一个最主要的地图应用.详细介绍请參考:百度地图SDK for Android[ ...
- springMVC的一些配置解析
<mvc:annotation-driven /> <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射--> 是一种简写形式,完全可以手 ...
- 相比于HTML4,HTML5废弃的元素有哪些?
第一类:表现性元素basefontbigcenterfontsstrikettu建议用语义正确的元素代替他们,并使用CSS来确保渲染后的效果 第二类:框架类元素因框架有很多可用性及可访问性问题,HTM ...
- Android使用token维持登陆状态的方法
什么是token token(令牌)是一串唯一的字符串,通常由服务端生成,在注册完成时返回给客户端,用来标识此用户,客户端将此字符串存储在本地.在以后的网络请求时,客户端先查询本地的token,如果有 ...
- startActivityForResult and onActivityResult
startActivityForResult and onActivityResult startActivityForResult 开启Activity 组织数据之后 发送,onActivityRe ...
- vue中剖析中的一些方法
1 判断属性 71 -81 var hasOwnProperty = Object.prototype.hasOwnProperty; /** * Check whether the object h ...
- Mysql学习总结(5)——MySql常用函数大全讲解
MySQL数据库中提供了很丰富的函数.MySQL函数包括数学函数.字符串函数.日期和时间函数.条件判断函数.系统信息函数.加密函数.格式化函数等.通过这些函数,可以简化用户的操作.例如,字符串连接函数 ...
- <LeetCode OJ> 20. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...