哪种设计模式利用了JavaScript的提升行为? [英] Which design pattern(s) take advantage of JavaScript's hoisting behavior?

查看:68
本文介绍了哪种设计模式利用了JavaScript的提升行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ben Cherry的优秀文章充分解释了用JavaScript提升。然而,我的问题是,我不能为这个臭名昭着的混淆犯罪者设想一个用例。请解释是否存在实际利用此语言功能的设计模式。

Ben Cherry's excellent article explains hoisting in JavaScript adequately. My problem, however, is that I cannot conceive a use case for this notorious perpetrator of confusion. Please explain if there is a design pattern that actually takes advantage of this language feature.

其次,JavaScript是否具有独特的范围提升?

Secondly, is scope hoisting unique to JavaScript?

UPDATE ---我正在为满足我的好奇心的答案添加赏金:哪些设计模式实际上利用了JavaScript的提升行为?我理解为什么 JavaScript支持提升,但我想知道我如何利用此功能

UPDATE --- I'm adding a bounty for an answer that satisfies my curiosity: Which design pattern(s) actually take advantage of JavaScript's hoisting behavior? I understand why JavaScript supports hoisting, but I want to know how I can take advantage of this feature.

推荐答案

可变提升



提升的最简单用途之一是可变提升。如果我们没有变量提升,则会抛出 ReferenceError

var bar = foo; 
var foo;

这似乎没有立即有用,但它允许我们这样做:

That doesn't seem immediately useful, but it allows us to do things like this:

var myCoolJS = myCoolJS || {};

这基本上意味着它的样子: myCoolJS myCoolJS 如果它存在,或者是新对象(如果不存在)。第二个 myCoolJS 如果 myCoolJS ,则不会抛出 ReferenceError 尚未存在,因为此变量声明已被提升。

This basically means what it looks like: myCoolJS is myCoolJS if it exists, or a new object if it doesn't. The second myCoolJS doesn't throw a ReferenceError if myCoolJS didn't already exist, because this variable declaration is hoisted.

这使我们免于做一个笨拙的 typeof myCoolJS!='undefined' check。

This saves us from doing an awkward typeof myCoolJS != 'undefined' check.

将多个脚本合并为一个时,功能提升特别有用。例如,我已经创建了 CommonJS模块的轻量级构建时实现。这提供了相同的模块 require exports 功能在node.js中找到我构建了工具,允许所需的模块由多个文件组成。例如, require('/ foo')可能会导致一个由两个文件组成的模块, foo.js ( 正文和 foo.h.js (头文件)。

Function hoisting can be especially useful when combining multiple scripts into one. For example, I've created a lightweight build-time implementation of CommonJS modules. This provides the same module, require, and exports features that are found in node.js. I built the tool to allow required modules to be composed of multiple files. For example, require('/foo') could result in a module composed of two files, foo.js (the "body file") and foo.h.js (the "header file").

这允许body文件不知道CommonJS模块环境提供的自由变量;所有这些都在标题中处理。这使得代码可以重复使用,并且无需构建即可轻松测试。但是,由于标题预先到正文,我们利用正文文件中的函数提升来允许标题中的导出。例如:

This allows the "body file" to have no knowledge of the free variables provided by the CommonJS modules environment; all of that is handled in the header. This makes code reusable and easy to test without building. However, since the headers are prepended to the body, we leverage function hoisting in the body file to allow exports in the headers. For example:

// dom.h.js

var util = require('util').util;

exports.css = css; // we can do this because "css" is hoisted from below

// ... other exports ...

...

// dom.js

function css(){}; // this would normally just be an object.

css.hasClass = function(element) { ... };
css.addClass = function(element) { ... };

// ...other code...

这篇关于哪种设计模式利用了JavaScript的提升行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆