我喜欢这个问题 - 函数构造函数的合法使用 - 所以我想创建一个关于Array构造函数的类似问题.
当然,数组文字表示法是创建数组的正确方法.这意味着new Array不应使用符号.并且"案件结案".
但是,new Array表格有一个特殊性.如果传入自然数,则会创建一个空数组,并将其length属性设置为该数字.
所以
arr = new Array( 7 );
Run Code Online (Sandbox Code Playgroud)
相当于
arr = [];
arr.length = 7;
Run Code Online (Sandbox Code Playgroud)
这可以被视为一项功能.我想知道这个"功能"是否具有实际用途.我最近偶然发现了一个这样的(简单)用法:
new Array( n + 1 ).join( '*' ) // returns string containing n stars
// e.g.
new Array( 3 ).join( '*' ) // returns '**'
new Array( 6 ).join( '*' ) // returns '*****'
Run Code Online (Sandbox Code Playgroud)
这很酷,但希望有一些更高级的用途.(可以使new Array符号成为JavaScript程序中合法工具的东西.)
更新:我注意到jQuery库new Array( len )在一个实例中使用了表示法 - 它在when …
今天我读过我们有一种通过Function构造函数声明函数的方法.但我从未见过真正使用Function构造函数的实际实现.所以我想问一下,有什么情况我们可以通过使用Function构造函数而不是使用function()声明来获益吗?两者之间隐藏的差异是什么?(如果有的话)
函数构造函数
var func = new Function("x", "y", "return x*y;"); // pass the context by String
Run Code Online (Sandbox Code Playgroud)
功能():
var func = function(x, y){ return x*y; }
Run Code Online (Sandbox Code Playgroud)
谢谢
with是如此糟糕,以至于在ES5严格模式下被禁止.
Function构造是非常缓慢的.
然而, John Resig的微模板被宣传为轻量级,使用了两个提到的Javascript的坏功能.(与另一个光模板相同- 下划线,它是它的一个分支)
现在有一个问题: 我应该多么认真地关注使用这些模板引擎对性能的影响?
我是否应该牺牲其灵活性和简单性来提高其他引擎(如Mustache或XTemplates)可能提供的速度?
我刚刚阅读async functions并发现了ES2017的一些类似功能.它造成了很多混乱,我想问一下:
async function,AsyncFunction和一个异步机能的研究表达式(这是用来创建一个异步函数),(这是我认为只是一个异步功能)?关于每个人的怪癖和表现的亮点将不胜感激!
根据这个基准 http://jsperf.com/function-vs-function 创建的函数运行速度提高了大约1000倍.你能评论一下吗?
如何动态地将函数附加到javascript对象.例如:如果动态附件的函数是attach(),那么我应该能够将函数fn附加到onject obj,如下所示.
attach(
obj,fn,{
alert(1)
}
)
function attach(obj,fnName,code)
{
obj[fnName] = code;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用经典的javascript原型继承,如下所示:
function Foo() {}
Naknek.prototype = {
//Do something
};
var Foo = window.Foo = new Foo();
Run Code Online (Sandbox Code Playgroud)
我想知道如何改进这个以及为什么我不能使用这个模型:
var Foo = window.Foo = new function() {
};
Foo.prototype = {
//Do something
};
Run Code Online (Sandbox Code Playgroud)
为什么这段代码不起作用?在我看来,这比经典的原型继承更合乎逻辑.
我查看了eslint-plugin-security中的一条规则,发现用户输入理论上可能会导致远程代码执行错误。
const a = class {};
console.log(a['constructor']);
a['constructor']('console.log(1)')();
function b() {}
console.log(b['constructor']);
b['constructor']('console.log(2)')();
const c = {}
console.log(c['constructor'])
console.log(c['constructor']('console.log(3)')());Run Code Online (Sandbox Code Playgroud)
从代码片段中很容易看出类和函数的构造函数似乎解析字符串并将它们评估为有效代码。由于某种原因,对象不会表现出这种行为。
为什么会允许这种情况发生?JavaScript 的哪些功能需要函数/类构造函数的这种行为?我假设它是 JavaScript 工作方式不可或缺的一部分,否则我不明白为什么它没有从语言中删除。
我听说在JavaScript中使用eval函数是个坏主意,但我有这个代码:
// this string (function) is generated dynamically...
var funVal = 'alert("a")' ;
//.....
var Button1 = document.getElementById("BtnSave");
// onclick event of button
eval("Button1.onclick = function () { " + funVal + " };");
Run Code Online (Sandbox Code Playgroud)
我不想用eval.还有其他解决方案吗?
javascript ×9
function ×2
arrays ×1
async-await ×1
constructor ×1
eval ×1
inheritance ×1
methods ×1
node.js ×1
object ×1
onclick ×1
prototype ×1
security ×1
templates ×1