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. 基于OpenCv的人脸检测、识别系统学习制作笔记之一

    基于OpenCv从视频文件到摄像头的人脸检测 在OpenCv中读取视频文件和读取摄像头的的视频流然后在放在一个窗口中显示结果其实是类似的一个实现过程. 先创建一个指向CvCapture结构的指针 Cv ...

  2. Jquery插件实现点击获取验证码后60秒内禁止重新获取

    通过jquery.cookie.js插件可以快速实现“点击获取验证码后60秒内禁止重新获取(防刷新)”的功能 先到官网(http://plugins.jquery.com/cookie/ )下载coo ...

  3. redhat 6上nis配置

    redhat 6上nis有点改动.在这里记一下 新装系统以后,首先要 yum install ypserv 安装ypserv的包 然后是设置 # 设置nis服务器名字 ypdomainname cen ...

  4. mysql 授权

    cd /usr/local/mysql/bin/grant all privileges on *.* to 'root'@'%' identified by '12345678';flush pri ...

  5. 发布一个简单的knockout-easyui绑定库

    最近做事情总是南辕北辙,拖延症越发严重了起来.原先计划早就要完成的这个项目也拖延了近两个月后总算勉勉强强发布了(最开始设想的部分功能就这么砍了,好吧纯粹个人太懒) knockout作为老牌的mvvm框 ...

  6. Asp.Net Web API 2第四课——HttpClient消息处理器

    Asp.Net Web API 导航   Asp.Net Web API第一课:入门http://www.cnblogs.com/aehyok/p/3432158.html Asp.Net Web A ...

  7. ASP.NET Web API 跨域访问

    自定义特性 要在WebApi中实现JSONP,一种方式是实现自定义特性  http://stackoverflow.com/questions/9421312/jsonp-with-asp-net-w ...

  8. Hive性能优化

    1.概述 继续<那些年使用Hive踩过的坑>一文中的剩余部分,本篇博客赘述了在工作中总结Hive的常用优化手段和在工作中使用Hive出现的问题.下面开始本篇文章的优化介绍. 2.介绍 首先 ...

  9. Java程序员的日常 —— static的用法讲解实践

    之前文章说过Java中static的作用,有朋友想看个例子.于是便抽空写了个小栗子 代码 package xing.test.thinking.chap5; class A{ public A() { ...

  10. atitit.提升备份文件复制速度(4) ---数据挖掘 获取回收站文件列表

    atitit.) ---数据挖掘 获取回收站文件列表 1. 放入回收站的原理and 1 2. info2文件文件结构 1 3. 获得文件列表2个法: 正则表达式or解析 1 4. 路径正则表达式[a- ...