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的更多相关文章

  1. struts2 CVE-2013-4316 S2-019 Dynamic method executions Vul

    catalog . Description . Effected Scope . Exploit Analysis . Principle Of Vulnerability . Patch Fix 1 ...

  2. Dynamic Method Resolution

    [Dynamic Method Resolution] @dynamic directive 用于声明属性的方法dynamic loading,which tells the compiler tha ...

  3. Dynamic Method Binding in Delphi 动态方法绑定

    Dynamic Method Binding in Delphi  动态方法绑定 https://docs.dataabstract.com/Delphi/AdvancedTopics/Dynamic ...

  4. 多态,动态方法调度(dynamic method dispatch)?

    8.多态Polymorphism,向上转型Upcasting,动态方法调度(dynamic method dispatch) 什么叫多态?简言之,马 克 - t o - w i n:就是父类引用指向子 ...

  5. DMI ( Dynamic Method Invocation )

    功能: 点击 hello , 调用 execute 函数 点击 update , 调用 update 函数 1.项目结构 2.web.xml <?xml version="1.0&qu ...

  6. .NET Core依赖注入集成Dynamic Proxy

    在<Castle DynamicProxy基本用法>中介绍了如何将DP与Autofac集成使用,而 .NET Core有自己的依赖注入容器,在不依赖第三方容器的基础上,如何实现动态代理就成 ...

  7. 深入浅出Cocoa之消息(二)-详解动态方法决议(Dynamic Method Resolution) 【转】

    序言 如果我们在 Objective C 中向一个对象发送它无法处理的消息,会出现什么情况呢?根据前文<深入浅出Cocoa之消息>的介绍,我们知道发送消息是通过 objc_send(id, ...

  8. 使用 C# (.NET Core) 实现模板方法模式 (Template Method Pattern)

    本文的概念内容来自深入浅出设计模式一书. 项目需求 有一家咖啡店, 供应咖啡和茶, 它们的工序如下: 咖啡: 茶: 可以看到咖啡和茶的制作工序是差不多的, 都是有4步, 其中有两步它们两个是一样的, ...

  9. 使用C# (.NET Core) 实现模板方法模式 (Template Method Pattern)

    本文的概念内容来自深入浅出设计模式一书. 项目需求 有一家咖啡店, 供应咖啡和茶, 它们的工序如下: 咖啡: 茶: 可以看到咖啡和茶的制作工序是差不多的, 都是有4步, 其中有两步它们两个是一样的, ...

随机推荐

  1. 第40讲:Set、Map、TreeSet、TreeMap操作代码实战

    今天来看下set map的操作,让我们从代码出发 val data = mutable.Set.empty[Int] data ++= List(1,2,3)//在空set上加入列表 data += ...

  2. C2第四次作业解题报告

    看过题解后如果觉得还算有用,请帮忙加点我所在团队博客访问量 http://www.cnblogs.com/newbe/ http://www.cnblogs.com/newbe/p/4069834.h ...

  3. Fire uva 11624

    题目连接:http://acm.hust.edu.cn/vjudge/problem/28833 /* 首先对整个图bfs一次得到火焰燃烧的时刻表 之后在bfs搜路径时加一个火烧表的判断 坑点在于:如 ...

  4. glRotate函数

    void glRotate( GLdouble angle, GLdouble x, GLdouble y, GLdouble z ) API说明:angle为旋转角度,单位为度:x,y,z是旋转轴的 ...

  5. EmberJs之Ember-Data

    写在前面 最近比较忙,换了新工作还要学习很多全新的技术栈,并给自己找了很多借口来不去坚持写博客.常常具有讽刺意味的是,更多剩下的时间并没有利用而更多的是白白浪费,也许这就是青春吧,挥霍吧,这不是我想要 ...

  6. Caliburn.Micro(CM) 穿过 Popup 绑定方法

    今天一个朋友,在用CM框架中,在一个ListView的DataTemplate,中用了个Popup,发现绑定不到VM(集合外的VM,即ListView的DataContext)中的方法了.我查了一下C ...

  7. HBase中MVCC的实现机制及应用情况

    MVCC(Multi-Version Concurrent Control),即多版本并发控制协议,广泛使用于数据库系统.本文将介绍HBase中对于MVCC的实现及应用情况. MVCC基本原理 在介绍 ...

  8. 深入理解c++构造函数, 复制构造函数和赋值函数重载(operator=)

    注 以下代码编译及运行环境均为 Xcode 6.4, LLVM 6.1 with GNU++11 support, Mac OS X 10.10.2 调用时机 看例子 // // main.cpp / ...

  9. 重装Windows系统后,Linux系统启动引导失败

    说明:我的电脑是在装了Windows系统后,装的Linux系统,所以启动引导是Linux干的活.可是,今天重装了Windows系统,启动引导就换成了Windows的,Windows的启动引导把Linu ...

  10. XML入门级的简单学习

    xml案例<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Ge ...