草庐IT

a_function_that_may_fail

全部标签

javascript - 在 Javascript 中围绕 Function() 创建沙箱

我可以限制字符串生成函数(使用Function构造函数)对父级/全局范围的访问吗?例如:下面的代码原样打印false,因为该函数正在存储/修改窗口中的变量a。window.a=4;Function("a=3;")()console.log(a===4);我可以限制对窗口/父范围的访问并让它打印出“true”吗? 最佳答案 这是一个额外的想法,与Esailija的提议一起可能会非常强大(请参阅他对讨论的回答的评论)。您可以创建虚拟iframe并使用其Function功能。默认情况下,用它创建的函数只能访问iframe的范围,尽管它仍然

javascript - 使用 PhantomJS 预渲染 AngualrJS 站点时出现 "Failed to instantiate module ngSanitize"

我正在尝试使用PhantomJS预呈现我的AngularJS网站。(使用来自http://www.yearofmoo.com/2012/11/angularjs-and-seo.html的phantomjs-runner.js)由于出现以下错误,我无法通过PhantomJS加载页面。在IE/Chrome/Firefox中不会出现此错误。我该如何解决这个错误?错误:Error:[$injector:modulerr]FailedtoinstantiatemoduleSpaceForAfricadueto:Error:[$injector:modulerr]Failedtoinstanti

javascript - 为什么我得到 ".push not a function"?

我的代码有什么问题?functionlongestConsec(strarr,k){varcurrentLongest="";varcounter=0;varoutPut=[];if(strarr.length===0||k>strarr.length||kcurrentLongest){currentLongest=strarr[i];}}while(currentLongest!==strarr[counter]){counter=counter+1}for(varj=0;j我一直收到“outPut.push不是一个函数”。 最佳答案

javascript - JavaScript 中 "anonymous function"和 "function literal"的区别?

本书LearningJavaScript定义匿名函数如下...Functionsareobjects.Assuch,youcancreatethem-justlikeaStringorArrayorothertype-byusingaconstructorandassigningthefunctiontoavariable.Inthefollowingcode,anewfunctioniscreatedusingtheFunctionconstructor,withthefunctionbodyandargumentpassedinasarguments:varsayHi=newFun

javascript - 不匹配的匿名 define() 模块 : function() {"use strict";return axe}

出于某种原因我有这个奇怪的错误:"Mismatchedanonymousdefine()module:function(){"usestrict";returnaxe}http://requirejs.org/docs/errors.html#mismatch"设置了一些JS断点后,发现错误源在这里:a[browserlink]Line363:/*!aXev2.0.5*Copyright(c)2016DequeSystems,Inc.*...etc...*/...etc...&&define([],function(){"usestrict";returnaxe}),...etc...

javascript - window.onload = function(){ .. }/window.onload = function(){ .. }(); 之间的区别

我在一个项目中使用了以下不起作用的代码:window.onload=function(){//codehere};但如果我在末尾添加()它会起作用:window.onload=function(){//codehere}();我的问题是,有什么区别?最后的()是什么?我认为第一个不起作用,因为在其他地方已经调用了“onload”来杀死这个。如果我总是使用第二个选项,它会有相同的行为吗? 最佳答案 ()在函数末尾,声明后立即调用此函数window.onload=function(){//codeehere}()//functionis

Javascript:var functionName = function() {} vs function functionName() {} 的性能

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:Doesuseofanonymousfunctionsaffectperformance?在Javascript中使用函数表达式与声明时,对解析和运行时的性能影响(如果有的话)是什么?例如以下两种方式的性能特点和区别:varfunctionOne=function(){//Somecode}或:functionfunctionTwo(){//Somecode}注意:问题不在于声明函数是否更快,而在于函数执行。

javascript - 浏览器调试 : how to see what Javascript functions was called after some action?

也许Firebug或Opera,GoogleChrome工具允许它。在哪里可以找到它?如何使用?示例:我单击按钮并想查看在该操作之后触发了哪些Javascript代码。 最佳答案 在GoogleChrome中,您可以打开开发者工具(Ctrl+Shift+J)选择Profiles选项卡,选择CollectJavaScriptCPUProfile,点击Start完成后,单击“停止”并查看进度日志... 关于javascript-浏览器调试:howtoseewhatJavascriptfunc

javascript - 如何为window.onbeforeunload自定义消息 "Changes you made may not be saved."?

我正在GoogleChrome中进行测试。我做了一些搜索,发现有人在使用:window.onbeforeunload=function(){if(hook){return"Didyousaveyourstuff?"}}但是当我使用它时,我仍然得到“您所做的更改可能无法保存”。信息。如何将其更改为我想要的内容? 最佳答案 你不能,这样做的能力在Chrome51中被移除了。这被广泛认为是一个安全问题,并且大多数vendor已经移除了支持。Custommessagesinonbeforeunloaddialogs(removed):Awi

javascript - Mongoose .js CastError : Cast to number failed for value "[object Object]" at path "undefined"

将Mongoose.js与node.js结合使用。我有这个架构:varPhoto=newSchema({URL:String,description:String,created_by:{type:ObjectId,ref:'User'},created_at:{type:Date,default:Date.now()}});varUser=newSchema({name:{type:String,index:true},email:{type:String,index:true,unique:true}});//TaskmodelvarTask=newSchema({title:St