[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步, 其中有两步它们两个是一样的, ...
随机推荐
- android listview用adapter.notifyDataSetChanged()无法刷新每项的图标
http://blog.csdn.net/caizhegnhao/article/details/41318575 今天在开发中遇到一个很奇怪的listview的问题. 这个问题情景是我的应用需要做一 ...
- Linux下paste命令
paste 用于将多个文件按照列队列进行合并. 该命令主要用来将多个文件的内容合并,与cut命令完成的功能刚好相反. 1.原文件: 1>a.txt [root@localhost home]# ...
- ASP.NET Core 源码阅读笔记(5) ---Microsoft.AspNetCore.Routing路由
这篇随笔讲讲路由功能,主要内容在项目Microsoft.AspNetCore.Routing中,可以在GitHub上找到,Routing项目地址. 路由功能是大家都很熟悉的功能,使用起来也十分简单,从 ...
- JWT【JSON Web Token】 简述
draft: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html http://tools.ietf.org/html/ ...
- 在Debian下编译Postgresql
今天起床看到床头上的那本<PostgreSQL数据库内核分析>,发觉这书买了这么长时间,虽然大致看了一遍,可还没亲手实践.今天就花了点时间搭了个调试环境. 环境:Debian 7.0 ## ...
- 学习WPF——初识依赖项属性
入门 首先创建一个依赖项属性 然后绑定父容器的DataContext到这个依赖项的实例 接着绑定子元素的属性到依赖项属性(注意Button的Content属性) 程序最终的运行结果: 说明 首先是 ...
- 在SecureCRT标签显示IP标题(转)
- HTML标签简明学习一
!-- ... -- html注释 浏览器不对其中的内容解析,可以用来调试及书写释意 <!-- 动不动就被注释 --> !DOCTYPE 声明文件类型 一般大写,必须位于文档首行,浏览器根 ...
- paip.Log4j配置不起作用的解决
paip.Log4j配置不起作用的解决 1.jar包里的log4j配置 看累挂jar,真的有个" webservices-rt.jar\com\sun\org\apache\xml\inte ...
- JavaScript 语句 for
循环for语句: for(var i =1;i<=5;i++)(初始条件:循环条件:状态改变) { 循环体 } 循环的作用:反复执行某段代码 四要素:初始条件.循环条件.循环体.状态改变 例题1 ...