我正在使用jQuery v.1.7.1,其中.live()方法显然已被弃用.
我遇到的问题是,当使用以下方法动态将html加载到元素中时:
$('#parent').load("http://...");
Run Code Online (Sandbox Code Playgroud)
如果我之后尝试添加click事件,则不会使用以下任一方法注册事件:
$('#parent').click(function() ...);
Run Code Online (Sandbox Code Playgroud)
要么
// according to documentation this should be used instead of .live()
$('#child').on('click', function() ...);
Run Code Online (Sandbox Code Playgroud)
实现此功能的正确方法是什么?它似乎只适用于.live(),但我不应该使用该方法.请注意,#child是动态加载的元素.
谢谢.
以下是实现相当简单的线程安全日志记录类的正确方法吗?
我知道我从未明确关闭过TextWriter,这会是一个问题吗?
当我最初使用该TextWriter.Synchronized方法时,它似乎没有工作,直到我在静态构造函数中初始化它并使它只读它:
public static class Logger
{
static readonly TextWriter tw;
static Logger()
{
tw = TextWriter.Synchronized(File.AppendText(SPath() + "\\Log.txt"));
}
public static string SPath()
{
return ConfigManager.GetAppSetting("logPath");
}
public static void Write(string logMessage)
{
try
{
Log(logMessage, tw);
}
catch (IOException e)
{
tw.Close();
}
}
public static void Log(string logMessage, TextWriter w)
{
w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
w.WriteLine(" :");
w.WriteLine(" :{0}", logMessage);
w.WriteLine("-------------------------------");
// Update the underlying file.
w.Flush();
}
}
Run Code Online (Sandbox Code Playgroud) 我需要一个特定的类来包含一个静态成员,该成员每次实例化该类的一个实例时都会跟踪,实质上是这样每个类的实例都有一个唯一的索引.它适用于非泛型类,但只要类型T在实例之间不同,这种通用实现就会失败:
class A<T>
{
private static int counter;
private static int Counter {
get {
Increment();
return counter;
}
}
private static void Increment() {
counter++;
}
public int Index;
public A()
{
this.Index = Counter; // using A<T>.Counter makes no difference
Console.WriteLine(this.Index);
}
}
class Program
{
static void Main(string[] args)
{
var a = new A<string>();
var b = new A<string>();
var c = new A<string>();
var d = new A<int>();
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
1
2
3
1 …
我已经读过,扩展System.Object通常是不好的做法,我同意这一点.
但是,如果以下内容被认为是一种有用的扩展方法,或者它仍然是不好的做法,我很好奇吗?
它类似于扩展System.Object但不完全相同,
public static R InvokeFunc<T, R>(this T input, Func<T, R> func)
{
return func.Invoke(input);
}
Run Code Online (Sandbox Code Playgroud)
这实际上允许任何对象调用任何将该对象作为参数并返回R的函数,无论该函数是否属于该对象.我认为这可能有助于一些有趣的"控制反转",但总体上并不确定.
思考?
如果你在javascript中有一个对象的实例,那么它似乎很难找到它的实际类型,即
var Point2D = function Point2D(x, y) {
return {
X: x,
Y: y
}
}
var p = new Point2D(1,1);
typeof p // yields just 'Object' not 'Point2D'
Run Code Online (Sandbox Code Playgroud)
我找到的一种方法是将对象作为自己的原型,然后通过调用prototype.constructor.name可以有效地获取其名称,
var Point2D = function Point2D(x, y) {
return {
X: x,
Y: y,
prototype: this
}
}
new Point2D(1,1).prototype.constructor.name // yields 'Point2D'
Run Code Online (Sandbox Code Playgroud)
这是不是一个好的方法(优点/缺点是什么?)还是我错过了更好的做法?
谢谢.
将私有静态类嵌套在非静态类中会被认为是一种不好的做法吗?
public class Outer
{
private static class Inner
{
}
}
Run Code Online (Sandbox Code Playgroud)
这里的想法是'Outer'的所有实例都将共享对静态的访问.另一种方法可能是让Inner类是非静态的并使用它的静态实例:
public class Outer
{
private static innerInstance = new Inner();
private class Inner
{
}
}
Run Code Online (Sandbox Code Playgroud)
效果相似.这种方法有哪些优缺点或其他考虑因素?
我必须承认,我几乎从不使用嵌套类,无论是否静态,但我对这个特定的概念感兴趣.
我正在使用Entity SQL查询我的EDM,并且在DateTime值上丢失了毫秒精度.例如2011/7/20 12:55:15.333 PM更改为2011/7/20 12:55:15.000 PM.
我已经确认在SQL中精确记录毫秒数.
我可以在.edmx XML文件中应用Precision属性,但我不知道它需要什么样的值,
<Property Name="Timestamp"
Type="DateTime"
Nullable="false"
Precision="???" />
Run Code Online (Sandbox Code Playgroud)
有谁知道如何使用这个精度属性?
谢谢.
在我看来,Javascript中不可变的类型是不可能的,或者有没有人知道创建它们的任何技巧?这是一种好的还是坏的做法?
例如,像,
var Point2D = function Point2D(x, y) {
var _x = x;
var _y = y;
function constructor() {
var self = {};
// Pseudo-Immutable concept
self.x = function() {
return _x;
}();
self.y = function() {
return _y;
}();
return self;
}
return constructor();
}
Run Code Online (Sandbox Code Playgroud)
这当然不是真正不可改变的,但如果它是1)有充分证据表明属性'x'和'y'是getter-functions或2)在验证不变性时抛出某种警报然后它可以作为一个事实上不可变的对象.
思考?
根据我对javascript的理解,原型方法无法访问构造函数范围内的私有变量,
var Foo = function() {
var myprivate = 'I am private';
this.mypublic = 'I am public';
}
Foo.prototype = {
alertPublic: function() { alert(this.mypublic); } // will work
alertPrivate: function() { alert(myprivate); } // won't work
}
Run Code Online (Sandbox Code Playgroud)
这很有道理,但有什么方法可以安全和良好的做法吗?由于使用原型提供了性能优势,因为成员函数只分配了一次,我想实现类似的功能,同时仍然可以访问我的私有变量.我不认为它会使用原型,但是有另一种模式,例如工厂方法或闭包方法吗?就像是,
var fooFactory = function() {
var _alertPrivate = function(p) { alert(p); } // bulk of the logic goes here
return function(args) {
var foo = {};
var myprivate = args.someVar;
foo.mypublic = args.someOtherVar;
foo.alertPrivate = function() { _alertPrivate(myprivate); };
return foo; …Run Code Online (Sandbox Code Playgroud) javascript constructor closures private prototype-programming
我正在编写一个试图复制本文开头讨论的算法的程序,
http://www-stat.stanford.edu/~cgates/PERSI/papers/MCMCRev.pdf
F是从char到char的函数.假设P1(f)是该函数的"合理性"度量.算法是:
从对函数的初步猜测开始,比如f,然后是新的函数f* -
我正在使用以下代码实现此功能.我正在使用c#,但试图让每个人都更加简化.如果有更好的论坛,请告诉我.
var current_f = Initial(); // current accepted function f
var current_Pl_f = InitialPl(); // current plausibility of accepted function f
for (int i = 0; i < 10000; i++)
{
var candidate_f = Transpose(current_f); // create a candidate function
var candidate_Pl_f = ComputePl(candidate_f); // compute its plausibility
if (candidate_Pl_f > current_Pl_f) // candidate Pl has improved
{
current_f = candidate_f; // accept the candidate
current_Pl_f = …Run Code Online (Sandbox Code Playgroud) c# algorithm functional-programming markov-chains montecarlo
c# ×6
javascript ×4
.net ×2
constructor ×2
generics ×2
oop ×2
static ×2
types ×2
algorithm ×1
closures ×1
datetime ×1
delegates ×1
dom ×1
events ×1
func ×1
handler ×1
immutability ×1
jquery ×1
logging ×1
montecarlo ×1
nested-class ×1
private ×1
typechecking ×1