[Core Javascirpt] Basic Metaprogramming: Dynamic Method
Somehow it looks like reflect in Java.
For example: We define an mothod on the Object, it called defineMethod(). It accepts two arguements, one is methodName andother is methodBody.
Read More: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
Using defineProperty() method of Object object to create method.
Object.prototype.defineMethod = function(methodName, methodBody){
Object.defineProperty(this, methodName, {
enumerable: true,
configurable: true,
value: methodBody
});
}
var dog = {breed: "Shelty"};
dog.defineMethod("bark", function(){
return "Woof!";
});
console.log(dog.breed);
console.log(dog.bark());
//"Shelty"
//"Woof!"
More useful case:
function User(){
User.statuses = ["inactive", "active"];
_.each(User.statuses, function(status){
this.defineMethod("is"+status.capitalize(), function(){
return this.status == status;
})
}, this);
}
var user = new User();
user.status = "active";
console.log(user.isActive());
console.log(user.isInactive());
//isActive() and isInactive() methods are created dynamcally during the running time.
Library: lodash and active-support.js
Read more: https://github.com/brettshollenberger/ActiveSupport.js/tree/master
https://egghead.io/lessons/core-javascript-basic-metaprogramming-dynamic-method
[Core Javascirpt] Basic Metaprogramming: Dynamic Method的更多相关文章
- struts2 CVE-2013-4316 S2-019 Dynamic method executions Vul
catalog . Description . Effected Scope . Exploit Analysis . Principle Of Vulnerability . Patch Fix 1 ...
- Dynamic Method Resolution
[Dynamic Method Resolution] @dynamic directive 用于声明属性的方法dynamic loading,which tells the compiler tha ...
- Dynamic Method Binding in Delphi 动态方法绑定
Dynamic Method Binding in Delphi 动态方法绑定 https://docs.dataabstract.com/Delphi/AdvancedTopics/Dynamic ...
- 多态,动态方法调度(dynamic method dispatch)?
8.多态Polymorphism,向上转型Upcasting,动态方法调度(dynamic method dispatch) 什么叫多态?简言之,马 克 - t o - w i n:就是父类引用指向子 ...
- DMI ( Dynamic Method Invocation )
功能: 点击 hello , 调用 execute 函数 点击 update , 调用 update 函数 1.项目结构 2.web.xml <?xml version="1.0&qu ...
- .NET Core依赖注入集成Dynamic Proxy
在<Castle DynamicProxy基本用法>中介绍了如何将DP与Autofac集成使用,而 .NET Core有自己的依赖注入容器,在不依赖第三方容器的基础上,如何实现动态代理就成 ...
- 深入浅出Cocoa之消息(二)-详解动态方法决议(Dynamic Method Resolution) 【转】
序言 如果我们在 Objective C 中向一个对象发送它无法处理的消息,会出现什么情况呢?根据前文<深入浅出Cocoa之消息>的介绍,我们知道发送消息是通过 objc_send(id, ...
- 使用 C# (.NET Core) 实现模板方法模式 (Template Method Pattern)
本文的概念内容来自深入浅出设计模式一书. 项目需求 有一家咖啡店, 供应咖啡和茶, 它们的工序如下: 咖啡: 茶: 可以看到咖啡和茶的制作工序是差不多的, 都是有4步, 其中有两步它们两个是一样的, ...
- 使用C# (.NET Core) 实现模板方法模式 (Template Method Pattern)
本文的概念内容来自深入浅出设计模式一书. 项目需求 有一家咖啡店, 供应咖啡和茶, 它们的工序如下: 咖啡: 茶: 可以看到咖啡和茶的制作工序是差不多的, 都是有4步, 其中有两步它们两个是一样的, ...
随机推荐
- Eclipse进行C/C++开发——Eclipse+CDT+MinGW的配置与使用详解
http://hi.baidu.com/ltb6w/item/3a51f11926fda60ce75c361d Eclipse进行C/C++开发——Eclipse+CDT+MinGW的配置与使用详解 ...
- [转]Java中的回车换行符/n /r /t
'\r'是回车,'\n'是换行,前者使光标到行首,后者使光标下移一格.通常用的Enter是两个加起来.下面转一篇文章. 回车和换行 今天,我总算搞清楚"回车"(carriage r ...
- ASP.NET 连接MySQL数据库 详细步骤
ASP.NET默认的数据库是MS SQL Server,微软的数据库产品.事实上,如果不计成本因素的话,Windows Server + IIS + MS SQL Server + ASP.NET是网 ...
- CodeFirst写界面——自己写客户端UI库
何谓CBS程序 CBS程序就是Client+Browser+Service的程序 纯CS程序写界面,有各种难处,那么我就在Client端引入Browser,让Browser渲染基于HTML的UI界面 ...
- thinkphp支持大小写url地址访问,不产生下划线
from:http://www.111cn.net/phper/thinkPhp/57748.htm 一.在配置文件中开启了thinkphp的大小写识别功能,使链接大小写都可以正常访问: ‘URL_C ...
- [WinAPI] API 5 [遍历驱动器并获取驱动器属性]
(1) GetLogicalDrives.获取主机中所有的逻辑驱动器,以BitMap的形式返回.◇返回值GetLogicalDrive函数返回一个DWORD类型的值,第一位表示所对应的驱动器是否存在. ...
- [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- SQL语法的重要知识点总结
好几年没写SQL语句了.现在到了新的team,需要用到数据库.作为QA的话时常需要使用客户端工具连接到数据库中找寻或修改数据.这么长时间没使用,一些SQL的使用技巧都忘得差不多了.晚上看了一些资料,花 ...
- win系统下nodejs安装及环境配置
第一步:下载安装文件下载nodejs,官网:http://nodejs.org/download/,我这里下载的是node-v0.10.28-x86.msi,如下图: 第二步:安装nodejs下载完成 ...
- SQL——用户定义函数
根据用户定义函数返回值的类型,可将用户定义函数分为如下三个类别: (1) 返回值为可更新表的函数 若用户定义函数包含单个 SELECT 语句且该语句可更新,则该函数返回的表也可更新,这样的函数称为内嵌 ...