我的背景是传统的编译面向对象语言,如C++和.NET编程,我现在正在尝试一些新项目的JavaScript.我正在涉足AJAX,并且遇到了浏览器如何管理对象的令人困惑的方面.
[编辑#2] - 更改活动内容脚本
我有一个带有三个按钮的练习页面,每个按钮都更新一个<textarea>使用XMLHttpRequest对象:
凡slowtime.php和fasttime.php是简单的脚本与两个时间戳返回文本/ HTML页面:一个页面加载时,一个在一段时间后已经过去.
每次单击一个按钮时,每个按钮都能正常工作.如果在第一个请求完成之前单击按钮2然后单击按钮3,则更新仍可按预期工作.
如果在第一个请求完成之前单击按钮1然后单击按钮2,则TextArea1和TextArea2将接收正确的值; 然而,onreadystatechange事件调用同时发生,即第一个响应延迟,并且仅在第二个响应到达时处理.
示例代码
网站
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc(url,target)
{
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(target).value=xmlhttp.responseText;
}
}
xmlhttp.open("POST",url,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<input type="button" value="Button 1" onClick="loadXMLDoc('slowtime.php','TextArea1')"/>
<input type="button" value="Button 2" onClick="loadXMLDoc('slowtime.php','TextArea2')"/>
<input type="button" value="Button 3" onClick="loadXMLDoc('fasttime.php','TextArea3')"/>
<div><textarea id="TextArea1"></textarea></div>
<div><textarea id="TextArea2"></textarea></div>
<div><textarea …Run Code Online (Sandbox Code Playgroud) 这个问题几乎与一年前未回答的问题相同:
我希望找到一个解决方案,并提供比原始问题更多的细节.
背景
我有一个服务器端的PHP脚本应该从表单或XHR请求中获取POST数据.当我使用提交表单测试网站时,它适用于Chrome和IE9.但是,当我使用XHR生成请求时,在客户端使用Chrome时,未定义php POST变量.这种行为是不一致的:在20次尝试中大约有1次,php确实接受了这些数据.
我检查了php://input流,发现在所有情况下都将帖子数据发送到服务器; 并注意到一小部分HTTP头($_SERVER)在测试用例之间是不同的.
码
服务器端:
<?php
echo file_get_contents("php://input") . "\n";
print_r($_SERVER);
print_r($_POST);
?>
Run Code Online (Sandbox Code Playgroud)
客户端表单版本(Chrome和IE9都可以使用)
<form action="scriptName.php" method="post">
Field: <input type="text" name="myField"><br>
<input type="submit" value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)
客户端XHR版本(IE9始终有效,Chrome工作时间约为5%)
<script>
function postToURL(url,data)
{
// Typical XHLHttpRequest declarations are removed for brevity
// - checks browser type and declares xmlhttp accordingly
// - defines a onreadystatechange handle
xmlhttp.open("POST",url,false);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(data);
}
</script>
...
<form>
<button type="button" onClick="postToURL('scriptname.php','myField=test');">
Test
</button>
</form>
Run Code Online (Sandbox Code Playgroud)
产量
在所有情况下,请求主体data( …
背景
最近,我观察到Winform ComboBox控件的两个不良行为:
DataSource属性设置为新对象会将SelectedIndex值设置为0DataSource属性设置为先前使用的对象可以“记住”先前的SelectedIndex值下面是一些示例代码来说明这一点:
private void Form_Load(object sender, EventArgs e)
{
string[] list1 = new string[] { "A", "B", "C" };
string[] list2 = new string[] { "D", "E", "F" };
Debug.Print("Setting Data Source: list1");
comboBox.DataSource = list1;
Debug.Print("Setting SelectedIndex = 1");
comboBox.SelectedIndex = 1;
Debug.Print("Setting Data Source: list2");
comboBox.DataSource = list2;
Debug.Print("Setting SelectedIndex = 2");
comboBox.SelectedIndex = 2;
Debug.Print("Setting Data Source: list1");
comboBox.DataSource = list1;
this.Close();
}
private …Run Code Online (Sandbox Code Playgroud) 所以今天我浏览ILSpy以更好地理解.NET如何在外部方法上执行DllImports,当我遇到奇怪的事情时:
当搜索枚举PInvokeImpl中定义的枚举值的System.Reflection.MethodAttributes引用时,我注意到了匹配的定义System.Reflection.FieldAttributes.
当然,这似乎不仅仅是幕后重用枚举值:System.Reflection.FieldInfo还有一个公开定义的属性IsPinvokeImpl,它专门检查是否设置了这个实现标志.
有趣的是,这个MethodInfo班级甚至没有这个属性 - 必须从MethodImplementationFlags酒店确定.
题:
实际上是否可以将字段实现为PInvoke,或者这只是.NET框架中的存根实现,以实现字段装饰和方法装饰之间的平衡?
如果可能,可以在C#中完成,还是这是一个需要C++/CLI的功能?
给出以下示例类:
class Foo<T>
{
void Bar<S>(T inputT, S inputS)
{
// Some really magical stuff here!
}
}
Run Code Online (Sandbox Code Playgroud)
如果我反思方法Foo<>.Bar<>(...),并检查参数类型,请说:
var argType1 = typeof(Foo<>).GetMethod("Bar").GetParameters()[0].ParameterType;
var argType2 = typeof(Foo<>).GetMethod("Bar").GetParameters()[1].ParameterType;
Run Code Online (Sandbox Code Playgroud)
既argType1与argType2外观相似:
FullName property为nullName 属性分别为"T"或"S"IsGenericParameter 是真的参数类型信息中是否有任何内容允许我区分第一个参数是在类型级别定义的,而第二个参数是方法级别的类型参数?
在C#中,有三种类型的数组:一维,锯齿状和多维矩形.
问题是:给定一个特定大小的数组,我们如何创建一个具有相同维度和排名的新数组?
在多维矩形阵列的情况下,似乎没有语法可以在运行时定义大小和等级(维数).
C#在索引器中声明带有逗号的多维数组:
object[,,] myArray = new object[2,4,2];
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我可以Rank通过调用GetLength方法并传递指定的维度来调用每个维度的属性和大小来确定数组的形状.
但是,即使我可以确定它myArray是2 x 4 x 2,如果我没有预先给出数组的等级,我怎么能以编程方式创建具有相同尺寸的数组的新实例?
背景
给定一个枚举变体类型(不可复制):
enum AstNode {
Op(Func, Box<AstNode>, Box<AstNode>),
Val(f64),
// others...
}
Run Code Online (Sandbox Code Playgroud)
尝试在两个这样的向量上运行操作:
fn apply_func_iterative(func: Func, lhs: Vec<AstNode>, rhs: Vec<AstNode>) -> Vec<AstNode> {
lhs.iter().zip(rhs).map(|(&l,r)| apply_func(func,l,r)).collect() // l and r are declared differently!
}
fn apply_func(func: Func, lhs: AstNode, rhs: AstNode) -> AstNode {
// magic happens here!
}
Run Code Online (Sandbox Code Playgroud)
在闭包中,lhs元素采用 type ,l: &AstNode而压缩rhs元素采用 type l: AstNode。(注意闭包元组中的不同声明)。
问题
为什么会这样?
有没有办法按值而不是按引用迭代向量的元素?从观察到的压缩元素的行为来看,这似乎是可能的。
(在这个例子中,这种差异导致语法声明有点奇怪,但在实践中,我在将引用变量传递给函数时遇到了借用检查器)
免责声明:我是 Rust 的新手
在C#中(可能在VB.NET中)有三种方法可以将常量值数组传递给函数,即:
byte[] buffer = {0};
someFunction(buffer);
Run Code Online (Sandbox Code Playgroud)
byte[] buffer = new byte[] {0};
someFunction(buffer);
Run Code Online (Sandbox Code Playgroud)
someFunction(new byte[] {0});
Run Code Online (Sandbox Code Playgroud)
而简单的类型转换数组声明符是无效的语法:
someFunction((byte[]) {0});
Run Code Online (Sandbox Code Playgroud)
题:
三种工作方法之间的性能差异是什么 - 在CPU使用率,内存分配和整体程序大小方面?使用new关键字是否会对RAM使用或分配产生任何影响,尤其是在函数调用之后声明的变量立即超出范围的情况下?