[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

时间: 2024-10-06 21:47:53

[Core Javascirpt] Basic Metaprogramming: Dynamic Method的相关文章

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

catalog 1. Description 2. Effected Scope 3. Exploit Analysis 4. Principle Of Vulnerability 5. Patch Fix 1. Description Dynamic Method Invocation is a mechanism known to impose possible security vulnerabilities, but until now it was enabled by default

DMI(Dynamic Method Invocation) 动态方法调用

创建action,内容如下: package action; import com.opensymphony.xwork2.ActionSupport; public class A extends ActionSupport { public String toJsp(){ return "success"; } } 配置struts.xml,内容如下: <?xml version="1.0" encoding="UTF-8" ?>

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

8.多态Polymorphism,向上转型Upcasting,动态方法调度(dynamic method dispatch) (视频下载) (全部书籍) 什么叫多态?简言之,马 克 - t o - w i n:就是父类引用指向子类时,父类和子类必须同时拥有某个同名函数,父类引用到底指向谁(调用谁的函数),是在runtime时决定的,因此呈现多种状态(不知道会指向若干子类中的哪一个还是父类自己).拿上一节的例子来讲,比如运行时如果用户输入自行车,就执行自行车的驾驶方法.如果用户输入小轿车,就执行小

DMI ( Dynamic Method Invocation )

功能: 点击 hello , 调用 execute 函数 点击 update , 调用 update 函数 1.项目结构 2.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/java

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

序言 如果我们在 Objective C 中向一个对象发送它无法处理的消息,会出现什么情况呢?根据前文<深入浅出Cocoa之消息>的介绍,我们知道发送消息是通过 objc_send(id, SEL, ...) 来实现的,它会首先在对象的类对象的 cache,method list 以及父类对象的 cache, method list 中依次查找 SEL 对应的 IMP:如果没有找到且实现了动态方法决议机制就会进行决议,如果没有实现动态方法决议机制或决议失败且实现了消息转发机制就会进入消息转发流

C# 动态语言特性,dynamic 关键字研究

原文:C# 动态语言特性,dynamic 关键字研究 1       动态语言简介 支持动态特性的语言现在大行其道,并且有继续增长的趋势.比如 Ruby 和 Python, 还有天王级的巨星 --- JavaScript. 现在一个程序员说自己对 JavaScript 根本没使用过,别人一定把你当成从火星回来的吧! 很多使用过 JavaScript 的程序员,刚开始对其动态特性深恶痛绝,欲除之而后快,但是一旦熟悉这个语言以后,又会发疯般的爱上她(我的野蛮女友). 以创建一个“人”为例, Java

Chapter 13 - Dynamic type and DLR

The content and code of this article is referenced from book Pro C#5.0 and the .NET 4.5 Framework by Apress. The intention of the writing is to review the konwledge and gain better understanding of the .net framework.    1. The role of C# dynamic key

C# Dynamic动态对象

1.ExpandoObject 1 dynamic expObj = new ExpandoObject(); 2 expObj.FirstName = "Daffy"; 3 expObj.LastName = "Duck"; 4 Console.WriteLine(expObj.FirstName + " " + expObj.LastName); 5 Func<DateTime, string> GetTomorrow = tod

debian下使用dynamic printk分析usb转串口驱动执行流程

看了一篇文章<debug by printing>,文中提到了多种通过printk来调试驱动的方法,其中最有用的就是"Dynamic debugging". “Dynamic debugging"的官方文档:http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/dynamic-debug-howto.txt?id=HEAD "Dyanmic de