我不知道你能做到这一点,直到我在一个棘手的bug上碰到我的头撞墙,最后发现我们失败了因为一些jquery插件覆盖了逃生功能.所以这将提出一个警告和docwrite null:
escape = function(a){alert(a)}
document.write(escape("Need tips? Visit W3Schools!"));
Run Code Online (Sandbox Code Playgroud)
凉!(不).
有没有办法恢复原生转义功能?
我有一个鼠标活动课程.我正在使用dojo b/c我喜欢它的OO方法
dojo.declare("MouseObject", null, {
constructor: function(){},
onclick : function(){...},
_onClick : function(){...}
});
Run Code Online (Sandbox Code Playgroud)
_onClick()侦听窗口生成的鼠标按下/释放事件并确定是否发生了单击.如果有,onClick()则被称为.onClick()执行所有可能的点击共同的功能,因此每次用户点击时都需要调用它.
有时用户可能想要扩展功能onClick()是否仍然存在原始功能而无需复制粘贴它?我能想到的两种方式,我都不喜欢
dojo.declare("MouseObjectChild", [MouseObject], {
constructor: function(){},
onclick : function(){this.inherited(arguments);...}
});
Run Code Online (Sandbox Code Playgroud)
这有一个缺点,我必须继续创建我不需要的新类,并且两个添加一个中间函数
dojo.declare("MouseObject", null, {
constructor: function(){},
onclick : function(){this._onClick()...}, //child onClick
_onClick : function(){...}, //parent onClick
__onClick : function(){...} //listener
});
Run Code Online (Sandbox Code Playgroud)
但这看起来不是很好的代码
作为赏金,我需要有关如何最好地解决程序 - 用户交互的专家建议.如果程序提供了一个关键功能,例如在画布上绘制一个圆圈,那么用户如何最好地与之交互.如果用户想在圆圈后面画一个三角形怎么办?圆圈前方的一个正方形?然后该程序必须提供前后方法,如下所示:
beforeDraw();
draw();
afterDraw();
Run Code Online (Sandbox Code Playgroud)
这被认为是好设计吗?我应该把函数指针放在一个数组中并按顺序调用它们吗?
我正在尝试调试仅在IE中发生的"未找到属性"异常.坏的部分是这个异常被捕获和处理,我只能弄清楚如何使IE 9调试器暂停未捕获的异常(Ctrl + Shift + E).
我不能暂时删除相关的try-catch子句而不会弄乱我的程序逻辑,我宁愿不必逐步手动操作调试器.如何轻松找到生成异常的行?
我有一个简单的问题.哈斯克尔正在向57 - Undefined variable "f" error我投掷,我不知道为什么.如果你能看一下,我会很感激的.
码:
eval :: Expr -> Environment -> Float
eval expr env = eval' expr
where
eval' :: Expr-> Float
eval' (Num num) = num
eval' (App app exprs) = foldl1 (f) (map eval' exprs) -- **Line 57**
eval' (Id id) = 5
where
f = getFunctionForApp app -- **f is here**
getFunctionForApp :: String -> (Float->Float->Float)
getFunctionForApp "+" = (+)
getFunctionForApp "-" = (-)
getFunctionForApp "*" = (*)
getFunctionForApp "/" = (/) …Run Code Online (Sandbox Code Playgroud) 如果我将一个变量传递给 Coffescript 中的存在运算符,它将转换为一对!==比较:
compiles to
Coffeescript ------> JS
a? typeof a !== "undefined" && a !== null;
Run Code Online (Sandbox Code Playgroud)
但如果我使用文字或表达式,它会使用!=比较:
compiles to
Coffeescript ------> JS
17? 17 != null;
//same thing for regexps, strings, function calls and other expressions
//as far as I know.
Run Code Online (Sandbox Code Playgroud)
除了可能让 JSLint 高兴之外,还有什么理由更喜欢 double!==而不是更短的 s吗?!= null
我在GitHub中查看Backbone-requireJS样板,我看到了两种不同类型的实现.
https://github.com/david0178418/BackboneJS-AMD-Boilerplate/blob/master/src/js/views/viewStub.js具有以下作为viewStub:
function() {
"use strict";
define([
'jqueryLoader',
'underscore',
'backbone',
],
function($, _, Backbone) {
return Backbone.View.extend({
template : _.template(/*loaded template*/),
initialize : function() {
this.render();
},
render : function() {
$(this.el).append(this.template(/*model/collection*/));
return this;
}
});
}
);
})();
Run Code Online (Sandbox Code Playgroud)
而来自其他样板的视图存根 https://github.com/jcreamer898/RequireJS-Backbone-Starter/blob/master/js/views/view.js具有以下内容:
define([
'jquery',
'backbone',
'underscore',
'models/model',
'text!templates/main.html'],
function($, Backbone, _, model, template){
var View = Backbone.View.extend({
el: '#main',
initialize: function(){
this.model = new model({
message: 'Hello World'
});
this.template = _.template( template, { model: this.model.toJSON() } );
}, …Run Code Online (Sandbox Code Playgroud) 我遇到过一种模式,我从种子值开始,x在每一步生成一个新的种子值和一个要输出的值.我想要的最终结果是输出值列表.这可以用以下函数表示:
my_iter :: (a -> (a, b)) -> a -> [b]
my_iter f x = y : my_iter f x'
where (x',y) = f x
Run Code Online (Sandbox Code Playgroud)
一个人为的例子就是生成Fibonacci数:
fibs:: [Integer]
fibs = my_iter (\(a,b) -> let c = a+b in ((b, c), c)) (0,1)
-- [1, 2, 3, 5, 8...
Run Code Online (Sandbox Code Playgroud)
我的问题是,我有这种感觉,很可能有更惯用的方式来做这种事情.我的功能的惯用替代方案是什么?
我现在唯一能想到的就是iterate前奏,但它们有一些缺点.
一种方法是先迭代然后映射
my_iter f x = map f2 $ iterate f1 x
where f1 = fst . f
f2 = snd . f
Run Code Online (Sandbox Code Playgroud)
但是,如果没有自然的方法将f分成单独的f1和f2函数,这看起来很难看.(在人为的Fibonacci案例中,这很容易做到,但是在某些情况下,生成的值不是种子的"独立"函数,因此分割事物并不那么简单)
另一种方法是将"输出"值与种子一起元组化,并使用单独的步骤将它们分开(有点像"Schwartzian变换"用于排序事物):
my_iter f …Run Code Online (Sandbox Code Playgroud) 建议在DOCTYPE之后,在根元素之前的任何注释之后,在html元素的开始标记之后(如果没有省略),以及在html元素内但在head元素之前的任何注释之后插入换行符. .
这个推荐背后的原因是什么?写作之间是否有区别?
<!DOCTYPE html>
<html>
<head>...
Run Code Online (Sandbox Code Playgroud)
和
<!DOCTYPE html><html><head>...
Run Code Online (Sandbox Code Playgroud)
?
如果我使用“传统”Javascript 类创建 Foo 类,则在控制台上打印 Foo 实例时,chrome 和 Firefox 都会显示 Foo 名称:
function Foo(){
this.x = 10;
}
console.log(new Foo());
// Foo {x: 10}
Run Code Online (Sandbox Code Playgroud)
另一方面,如果我使用手工滚动原型继承,那么在调试时我不会得到有用的名称
function mkClass(init, proto){
return function(/**/){
var obj = Object.create(proto);
init.apply(obj, arguments);
return obj;
}
}
var Bar = mkClass(function(){ this.x = 10 }, {});
console.log(Bar());
// Object {x: 10}
Run Code Online (Sandbox Code Playgroud)
有没有办法让通过我的原型系统创建的类在控制台上打印时显示它们的名称?到目前为止,我能想到的唯一方法是一个丑陋的黑客滥用 eval 为 mkClass 返回的当前匿名构造函数提供不同的名称。
我必须在Haskell中对大整数矩阵的行进行排序,然后我开始使用随机数据进行基准测试.我发现Haskell比C++慢3倍.
由于随机性,我希望行比较总是在第一列终止(它应该没有重复).因此,我将矩阵缩小为实现为Vector(Unboxed.Vector Int)的单个列,并将其排序与通常的Vector Int进行比较.
Vector Int尽可能快地排序C++(好消息!),但同样,列矩阵慢了3倍.你知道为什么吗?请在下面找到代码.
import qualified Data.Vector.Unboxed as UV(Vector, fromList)
import qualified Data.Vector as V(Vector, fromList, modify)
import Criterion.Main(env, bench, nf, defaultMain)
import System.Random(randomIO)
import qualified Data.Vector.Algorithms.Intro as Alg(sort)
randomVector :: Int -> IO (V.Vector Int)
randomVector count = V.fromList <$> mapM (\_ -> randomIO) [1..count]
randomVVector :: Int -> IO (V.Vector (UV.Vector Int))
randomVVector count = V.fromList <$> mapM (\_ -> do
x <- randomIO
return $ UV.fromList [x]) [1..count]
benchSort :: IO ()
benchSort = do …Run Code Online (Sandbox Code Playgroud) javascript ×6
haskell ×3
backbone.js ×1
coffeescript ×1
debugging ×1
dojo ×1
eval ×1
function ×1
html ×1
html5 ×1
matrix ×1
performance ×1
redefine ×1
requirejs ×1
reusability ×1
sorting ×1