Javascript 方法执行顺序

标签 javascript yeoman

在阅读 yeoman 生成器文档 ( http://yeoman.io/generators.html#writing-your-first-generator ) 时,我遇到了以下行。

从顶部开始,您放置在 BlogGenerator.prototype 上的每个方法都将按照您编写它们的顺序被调用。

yeoman 如何以相同的顺序执行方法?

最佳答案

其实,这很简单……

var methods = Object.keys(Object.getPrototypeOf(this));

这是 Github link .是的,它们被调用,而不是被评估:

/*
 * Runs the generator, executing top-level methods in the order they
 * were defined.
 */
// ...
async.series(methods.map(resolve), runHooks); 

从技术上讲,如果用数字名称定义属性,可能会遇到麻烦。例如:

var Foo = function() {};
Foo.prototype.bar  = function() { console.log('I am bar'); };
Foo.prototype[123] = function() { console.log('I am 123'); };

var foo = new Foo(); 
var methods = Object.keys(Object.getPrototypeOf(foo)); // ['123', 'bar']
methods.map(function(fn) { foo[fn]() }); 
// I am 123
// I am bar

但我假设这些情况被认为是不存在的。对于所有其他人,看起来 V8 正在遵守属性的插入顺序 - 否则给定的代码显然不会按预期运行。

关于Javascript 方法执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19637563/

相关文章:

javascript - 通过 javascript 将文本文件转换为 html

任意编号 li 的 JavaScript

html - iPad2、iPhone 4S 上的 iOS 网站加载问题

angularjs - 是否有任何自动化的 [grunt] 任务可以在 index.html 中为 CSS/JS 文件添加 CDN?

javascript - Div.Show 在运行时不起作用,但在单步执行代码时起作用

javascript - 在数据库服务器中存储 UDID - Android

git - Yeoman 安装和协作(例如通过 git)

java - 从 JDL 文件添加 jHipster 实体时出错

gruntjs - Yeoman & Grunt,如何仅包含在构建中

javascript - 环回 : Executing a generated method using JavaScript