我最近Object.create()在JavaScript中偶然发现了这个方法,并试图推断它与创建一个对象的新实例有什么不同new SomeFunction(),当你想要使用另一个时.
请考虑以下示例:
var test = {
val: 1,
func: function() {
return this.val;
}
};
var testA = Object.create(test);
testA.val = 2;
console.log(test.func()); // 1
console.log(testA.func()); // 2
console.log('other test');
var otherTest = function() {
this.val = 1;
this.func = function() {
return this.val;
};
};
var otherTestA = new otherTest();
var otherTestB = new otherTest();
otherTestB.val = 2;
console.log(otherTestA.val); // 1
console.log(otherTestB.val); // 2
console.log(otherTestA.func()); // 1
console.log(otherTestB.func()); // 2Run Code Online (Sandbox Code Playgroud)
请注意,在两种情况下都观察到相同的行为.在我看来,这两种情况之间的主要区别是:
Object.create()实际使用的对象实际上形成了新对象的原型,而在new …当用户点击链接时,我需要更新数据库中的字段,然后在新窗口中打开请求的链接.更新没问题,但我不知道如何打开一个新窗口而不要求他们点击另一个超链接.
<body onLoad="document.getElementById('redirect').click">
<a href="http://www.mydomain.com?ReportID=1" id="redirect" target="_blank">Report</a>
</body>
Run Code Online (Sandbox Code Playgroud) C#,.NET 4.0,VS2010.
WPF新手.我的MainWindow上有一个ComboBox.我挂钩了所说组合框的SelectionChanged事件.但是,如果我检查事件处理程序中组合框的值,它具有旧值.这听起来更像是一个"SelectionChanging"事件,而不是SelectionChanged事件.
如何在选择实际发生后获取ComboBox的新值?
目前:
this.MyComboBox.SelectionChanged += new SelectionChangedEventHandler(OnMyComboBoxChanged);
...
private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e)
{
string text = this.MyComboBox.Text;
}
Run Code Online (Sandbox Code Playgroud)
注意,如果我使用在事件args,egeOriginalSource中传递的对象,我会得到相同的行为.
我觉得我对闭包有很好的理解,如何使用它们,以及它们什么时候有用.但我不明白的是他们实际上是如何在幕后的幕后工作的.一些示例代码:
public Action Counter()
{
int count = 0;
Action counter = () =>
{
count++;
};
return counter;
}
Run Code Online (Sandbox Code Playgroud)
通常,如果闭包没有捕获{count},它的生命周期将被限定为Counter()方法,并且在它完成之后它将消除Counter()的其余堆栈分配.什么时候关闭会发生什么?这个Counter()调用的整个堆栈分配是否存在?它会将{count}复制到堆中吗?它是否从未真正在堆栈上分配,但被编译器识别为关闭,因此总是存在于堆上?
对于这个特殊的问题,我主要关注它在C#中是如何工作的,但是不反对与支持闭包的其他语言进行比较.
我正在使用C#,.NET 3.5.我理解如何利用事件,如何在我的班级中声明它们,如何将它们从其他地方挂钩等等.一个人为的例子:
public class MyList
{
private List<string> m_Strings = new List<string>();
public EventHandler<EventArgs> ElementAddedEvent;
public void Add(string value)
{
m_Strings.Add(value);
if (ElementAddedEvent != null)
ElementAddedEvent(value, EventArgs.Empty);
}
}
[TestClass]
public class TestMyList
{
private bool m_Fired = false;
[TestMethod]
public void TestEvents()
{
MyList tmp = new MyList();
tmp.ElementAddedEvent += new EventHandler<EventArgs>(Fired);
tmp.Add("test");
Assert.IsTrue(m_Fired);
}
private void Fired(object sender, EventArgs args)
{
m_Fired = true;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我不明白的是,当一个人声明一个事件处理程序时
public EventHandler<EventArgs> ElementAddedEvent;
Run Code Online (Sandbox Code Playgroud)
它从未被初始化 - 所以,究竟是什么是ElementAddedEvent?它指向什么?以下操作无效,因为EventHandler永远不会被初始化:
[TestClass]
public class …Run Code Online (Sandbox Code Playgroud) jQuery 1.3.2,ASP.NET 2.0.对PageMethod(WebMethod)进行AJAX调用会返回完整/整个页面,而不仅仅是响应.页面方法上的断点显示它永远不会被击中.我的方法有[WebMethod]属性,它是 public static,返回string并且不接受params.我甚至尝试在我的班级顶部添加[ScriptService]以查看它是否有帮助,但事实并非如此.
我已经看到这篇帖子Jquery AJAX与ASP.NET WebMethod返回整个页面有相同的症状,但我仍然有问题.我读了http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/,我觉得我跟着这个到了T,但仍然没有运气.
我正在制作的jQuery调用是:
jQuery.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: '{}',
dataType: 'json',
url: 'MyPage.aspx/SomePageMethod',
success: function(result){
alert(result);
}
});
Run Code Online (Sandbox Code Playgroud)
根据FF3中的Firebug,请求/响应头如下所示
Response Headers
Server ASP.NET Development Server/8.0.0.0
Date Tue, 24 Feb 2009 18:58:27 GMT
X-AspNet-Version 2.0.50727
Cache-Control private
Content-Type text/html; charset=utf-8
Content-Length 108558
Connection Close
Request Headers
Host localhost:2624
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6
Accept application/json, text/javascript, */*
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset …Run Code Online (Sandbox Code Playgroud) C#,.NET 3.5
我试图获得一个对象的所有属性,它们具有实例的getter和setter.我认为应该工作的代码是
PropertyInfo[] infos = source.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.GetProperty);
Run Code Online (Sandbox Code Playgroud)
但是,结果包括没有setter的属性.为了简单介绍一下可能影响它的继承结构(虽然我不知道如何):
public interface IModel
{
string Name { get; }
}
public class BaseModel<TType> : IModel
{
public virtual string Name { get { return "Foo"; } }
public void ReflectionCopyTo(TType target)
{
PropertyInfo[] infos = this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.GetProperty);
foreach (PropertyInfo info in infos)
info.SetValue(target, info.GetValue(this, null), null);
}
}
public class Child : BaseModel<Child>
{
// I do nothing to …Run Code Online (Sandbox Code Playgroud) 我正在使用C#和.NEt 3.5.OptionA和OptionB有什么区别?
class MyClass
{
private object m_Locker = new object();
private Dicionary<string, object> m_Hash = new Dictionary<string, object>();
public void OptionA()
{
lock(m_Locker){
// Do something with the dictionary
}
}
public void OptionB()
{
lock(m_Hash){
// Do something with the dictionary
}
}
}
Run Code Online (Sandbox Code Playgroud)
我开始涉足线程化(主要是为多线程应用程序创建缓存,不使用HttpCache类,因为它没有附加到网站上),我在很多例子中都看到了OptionA语法在线看,但我不明白在OptionB上做了什么,如果有的话.
我有2个表 - 一个Account表和一个Users表.每个帐户可以有多个用户.我有一个场景,我想对这两个表执行单个查询/连接,但我想要所有的帐户数据(帐户.*)和只有第一组用户数据(特别是他们的名字).
我没有在我的聚合小组上做"最小"或"最大",而是想做一个"第一次".但是,显然,TSQL中没有"First"聚合函数.
有关如何获取此查询的任何建议?显然,很容易获得Account x Users的笛卡尔积:
SELECT User.Name, Account.* FROM Account, User
WHERE Account.ID = User.Account_ID
Run Code Online (Sandbox Code Playgroud)
但是我怎样才能根据User.ID的顺序从产品中获取第一个用户?
一位同事今天问我一个有趣的问题 - C#关键字/运算符"是否被认为是反思?
object tmp = "a string";
if(tmp is String)
{
}
Run Code Online (Sandbox Code Playgroud)
这个操作符是如何在幕后实现的?它需要反思还是内省?或者由于语言的强类型性质,对象的类型是否可以作为内存中对象的顶级属性立即访问?
MSDN声明:
请注意,is运算符仅考虑引用转换,装箱转换和拆箱转换.is运算符不考虑其他转换,例如用户定义的转换.
考虑盒装和非盒装转换的能力似乎意味着某种内省.
c# ×6
.net ×4
javascript ×2
reflection ×2
.net-3.5 ×1
.net-4.0 ×1
asmx ×1
asp.net ×1
closures ×1
combobox ×1
delegates ×1
events ×1
jquery ×1
pagemethods ×1
prototype ×1
t-sql ×1
typing ×1
web-services ×1
wpf ×1