AOP(面向切面的编程)主要是将一些与核心业务逻辑模块无关的功能抽离出来,这些功能通常包括日志统计,安全控制,或者是异常处理等等. 我们要做的就是拓展Function.prototype来“动态植入”到业务的逻辑模块儿中,保持业务逻辑的纯净和高内聚. 现在我们有一个函数 var myFunc = function(){ console.log(1); } myFunc(); //1 那我们如何植入一个函数,让他在这个函数执行之前执行呢? 现在我们来拓展一个before函数. var myFunc…
一.前言                                大家先预计一下以下四个函数调用的结果吧! var test = function(){ console.log('hello world') return 'fsjohnhuang' }test.call() // ① Function.prototype.call(test) // ② Function.prototype.call.call(test) // ③ Function.prototype.call.call(…
一.前言                                大家先预计一下以下四个函数调用的结果吧! var test = function(){ console.log('hello world') return 'fsjohnhuang' }test.call() // ① Function.prototype.call(test) // ② Function.prototype.call.call(test) // ③ Function.prototype.call.call(…
关于javascript的原型链有一个问题我一直很疑惑:为什么 Function instanceof Object 与 Object instanceof Function都为true呢? Function instanceof Object // ->trueObject instanceof Function // ->true12先给个结论吧(函数a和对象b指什么看下去就直到了):Object instanceof Function的实质就是函数a在Object的原型链上:Functi…
重新看<JavaScript设计模式与开发实践>一书,第32页发现个简易版的Function.prototype.bind实现,非常容易理解,记录在这了. Function.prototype.bind = function (context) { var self = this; return function () { return self.apply(context, arguments); }; }; var obj = { name: '前端架构师' }; var func = f…
Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello") } console.log( hello.toString() ); 输出: 'function hello( msg ){ \ console.log("hello") \ }' 这个方法真是碉堡了-, 通过合适的正则, 我们可以从中提取出丰富的信息. 函数名 函数形参列表…
Object.prototype 方法: hasOwnProperty 概念:用来判断一个对象中的某一个属性是否是自己提供的(主要是判断属性是原型继承还是自己提供的) 语法:对象.hasOwnProperty('属性名') var o = { name: 'jim' }; function Person() { this.age = 19; this.address='北京'; this.work='上海'; } Person.prototype = o; var p = new Person(…
方法: hasOwnProperty isPrototypeOf propertyIsEnumerable hasOwnProperty 该方法用来判断一个对象中的某一个属性是否是自己提供的( 住要用在判断属性是原型继承的还是自己提供的 ) 语法: 对象.hasOwnProperty( '属性名' ) -> boolean isPrototypeOf 凡是看到 of 翻译成 的, 反过来翻译: prototype of obj, 翻译成 obj 的 原型 因此该方法的含义是: xxx 是 xxx…
昨天边参考es5-shim边自己实现Function.prototype.bind,发现有不少以前忽视了的地方,这里就作为一个小总结吧. 一.Function.prototype.bind的作用 其实它就是用来静态绑定函数执行上下文的this属性,并且不随函数的调用方式而变化. 示例: test('Function.prototype.bind', function(){ function orig(){ return this.x; }; var bound = orig.bind({x: '…
本文大部分内容翻译自 MDN内容, 翻译内容经过自己的理解. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind Function.prototype.bind Syntax fun.bind(thisArg[, arg1[, arg2[, ...]]]) Parameters thisArg The value to be passed as the thi…