It’s very easy to do reflection in Javascript. Reflection is when your code looks onto itself to discover its variables and functions. It allows two different Javascript codebases to learn about each other, and it’s useful for exploring third-party APIs.
Preamble
In Javascript, all objects are hashes/associative arrays/dictionaries. A hash is like an array, except that values are associated with unique key string rather than a numeric index.
Adding a new variable to an object is as simple as assigning a new value to a key in the object.
You can declare everything in place:
var o = {
count: 0,
name: 'Jane Doe',
greeting: function() { return "Hi"; }
};
o.greeting();
Or you can start with a blank object and assign things later:
var o = {};
o.count = 0;
o.name = 'Jane Doe';
o.greeting = function() { return "Hi"; };
o.greeting();
Or you can do the same, but in a different notation:
var o = {};
o['count'] = 0;
o['name'] = 'Jane Doe';
o['greeting'] = function() { return "Hi"; }
o.greeting();
The last is especially handy because it allows you to go from the string name of the variable to the variable without the performance penalties ofeval()
.
{}
is synonymous withnew Object()
.
Reflection
The loop below pops up a dialog box with the name and value of every variable in object:
for (var member in object) {
alert('Name: ' + member);
alert('Value: ' + object[member]);
}
Everything in Javascript is an object. Functions are objects!document
andwindow
are objects. The most reliable reference for Javascript in a given
browser is reflection into its innards.
Finally, some slightly more useful code:
/**
Returns the names of all the obj's
variables and functions in a sorted
array
*/
function getMembers(obj) {
var members = new Array();
var i = 0;
for (var member in obj) {
members[i] = member;
i++;
}
return members.sort();
}
/**
Print the names of all the obj's variables
and functions in an HTML element with id
*/
function printMembers(obj, id) {
var members = getMembers(obj);
var display = document.getElementById(id);
for (var i = 0; i < members.length; i++) {
var member = members[i];
var value = obj[member];
display.innerHTML += member + ' = ';
display.innerHTML += value + '<br>';
}
}
More sophisticated uses are left as an exercise for the reader. :-)
分享到:
相关推荐
console.log("Hello, Reflection in JavaScript!"); }; let obj = new MyClass(); let proto = Object.getPrototypeOf(obj); let method = proto.myMethod; method.call(obj); ``` 这个例子展示了如何获取`...
Reflection Section 3.7. Enumeration Section 3.8. Delete Section 3.9. Global Abatement Chapter 4. Functions Section 4.1. Function Objects Section 4.2. Function Literal Section 4.3. Invocation ...
## Your Challenge Reflection是面向对象语言中的通用实用程序,用于检查类并派生有关其公开的属性/方法以及它们从其继承的其他类的信息。 今天我们将编写我们自己JavaScript反射器! 我们将编写一个简单的实用程序...
基于PHP文件的Nodejs Reflection API 安装 npm install php-reflection --save 用法 var reflection = require ( 'php-reflection' ) ; var workspace = new reflection .... '*.inc' , '*.class' , '*.req'
You will also learn about the collection framework and how to communicate with the different programs written in JavaScript using Dart. This book will show you how to add internalization support to ...
ECMAScript 6 is the new edition to the ECMAScript language, whose specifications are inherited by JavaScript. ES6 gives a vast makeover to JavaScript by adding new syntaxes and APIs to write complex ...
based, object-oriented language that simplifies the development of structured modern apps, scales from small scripts to large applications, and can be compiled to JavaScript for use in any modern ...
based, object-oriented language that simplifies the development of structured modern apps, scales from small scripts to large applications, and can be compiled to JavaScript for use in any modern ...
综上所述,`Object`是JavaScript的核心组成部分,`Reflection`提供了对对象内部结构的洞察,而`for...in`循环则为遍历和操作对象提供了便利。掌握这些知识点,对于提升JavaScript编程技能至关重要。在日常工作中,...
1. **高性能**:Nashorn比Rhino更快,因为它基于Java的HotSpot虚拟机,能够利用JIT(Just-In-Time)编译器进行优化。 2. **JS与Java互操作性**:Nashorn允许JavaScript代码直接调用Java对象和方法,反之亦然,使得...
Next, it will teach you how to write asynchronous code in a synchronous style using ES6.Moving on, it will teach you how to create reflection objects, use it to expose hidden object properties, and ...
- JavaScript反射工具(JavaScript Reflection Tools) - 文档化Kotlin代码(Documenting Kotlin Code) - 使用Kotlin注解处理工具(Using Kotlin annotation processing tool) - 使用Gradle(Using Gradle) - ...
12.5 JavaScript to sneakily send POSTs 330 13.1 Example sequence of letters 341 13.2 Looking at Word documents with emacs 342 13.3 Interesting relics in the binary 342 13.4 Turning Fast Save off ...
You can check the status variable of the applet while it is loading to determine if the applet is ready to handle requests from JavaScript code; see Handling Initialization Status With Event Handlers....
The book does not assume prior knowledge of Go nor experience with any specific language, so you’ll find it accessible whether you’re most comfortable with JavaScript, Ruby, Python, Java, or C++. ...
An In-Depth Look at XMLHttpRequest Creating the Object Asynchronous Data Transfers The Ready State HTTP Status Codes and Headers Chapter 3. The Response XML JSON Chapter 4. Rendering ...
5. **ssfx.Reflection.dll**:提供了反射工具,用于操作对象结构。 6. **ssagctrl.dll**:封装了Microsoft Silverlight XAML DOM。 7. **ssve4.dll**:支持Microsoft Virtual Earth APIs。 8. **ssgadgets.dll**:...