根据这篇文章,当在 TypeScript 中启用严格的空值检查时,除非联合明确允许,否则您不能分配null或分配undefined给变量。
// required value
let req: string;
req = "Something"; // OK
req = null; // Error
req = undefined; // Error
// nullable value
let nbl: string | null;
nbl = "Something"; // OK
nbl = null; // OK
nbl = undefined; // Error
Run Code Online (Sandbox Code Playgroud)
但是在 TypeScript 中null允许在可选值中使用吗?
// optional value
let opt?: string; // (actually invalid, as optional types cannot be used for variable declarations, but that's …Run Code Online (Sandbox Code Playgroud) 在 TypeScript 中,您可以将函数注释为返回void:
function fn1(): void {
// OK
}
function fn2(): void {
// Error
return 3;
}
Run Code Online (Sandbox Code Playgroud)
您还可以注释要返回的函数undefined:
function fn3(): undefined {
// OK
return;
}
function fn4(): undefined {
// Error
return 3;
}
Run Code Online (Sandbox Code Playgroud)
所以看起来如果你调用一个返回的函数void,你总是会得到值undefined。但是你不能写这个代码:
function fn5(): void {
}
let u: undefined = fn5(); // Error
Run Code Online (Sandbox Code Playgroud)
为什么不void只是 的别名undefined?它真的需要存在吗?
我有一些关于从函数返回对局部变量的引用的问题:
class A {
public:
A(int xx)
: x(xx)
{
printf("A::A()\n");
}
};
const A& getA1()
{
A a(5);
return a;
}
A& getA2()
{
A a(5);
return a;
}
A getA3()
{
A a(5);
return a;
}
int main()
{
const A& newA1 = getA1(); //1
A& newA2 = getA2(); //2
A& newA3 = getA3(); //3
}
Run Code Online (Sandbox Code Playgroud)
我的问题是=>
执行getA1()是否正确?我觉得它是错误的,因为它返回局部变量的地址或临时变量.
main(1,2,3)中的哪些陈述会导致未定义的行为?
在const A& newA1 = getA1();做标准的保证,暂时由const引用约束不会被破坏,直到引用超出范围是什么?
undefined和 之间究竟有什么区别void 0?
哪个是首选,为什么?
我正在尝试为过渡创建一个sass mixin.这就是我到目前为止所拥有的.
@mixin transition($var)
-webkit-transition: $var
transition: $var
Run Code Online (Sandbox Code Playgroud)
我希望能够像这样传递多个参数
@include transition(color .5s linear, width .5s linear)
Run Code Online (Sandbox Code Playgroud)
不幸的是,我收到以下错误
Syntax error: Mixin transition takes 1 argument but 2 were passed.
Run Code Online (Sandbox Code Playgroud)
有没有办法这样做,所以它在css中产生以下输出,同时仍然接受一个未定义数量的参数?
-webkit-transition: color .5s linear, width .5s linear;
transition: color .5s linear, width .5s linear;
Run Code Online (Sandbox Code Playgroud) 我已经看到了声明变量并设置为undefined和null的不同代码示例.如:
var a; // undefined - unintentional value, object of type 'undefined'
var b = null; // null - deliberate non-value, object of type 'object'
Run Code Online (Sandbox Code Playgroud)
如果遵循这些声明的代码为a或b赋值,那么使用一种声明而不是另一种声明的原因是什么?
这段代码好吗?
var wlocation = $(this).closest('.myclass').find('li a').attr('href');
if (wlocation.prop !== undefined) { window.location = wlocation; }
Run Code Online (Sandbox Code Playgroud)
或者我应该这样做
var wlocation = $(this).closest('.myclass').find('li a').attr('href');
if (wlocation.prop !== "undefined") { window.location = wlocation; }
Run Code Online (Sandbox Code Playgroud) 给出以下数组:
var arr = [undefined, undefined, 2, 5, undefined, undefined];
Run Code Online (Sandbox Code Playgroud)
我想获得哪些元素的计数的定义(即:那些不 undefined).除了遍历数组之外,还有一个很好的方法吗?
我试图测试是否未定义Javascript变量.
你会看到我并不期望predQuery [preId]的值是'未定义的'如果我没有先得到一个警告说"它难以置信".但我经常这样做,所以我猜我的说法
predQuery[preId]=='undefined')
Run Code Online (Sandbox Code Playgroud)
未正确匹配未定义的元素.
if((predQuery.length < preId) || (predQuery[preId]=="") || (predQuery[preId]=='undefined')){
alert("its unbelievable");
alert(predQuery[preId]);
queryPreds[variables] = preId;
queryObjs[variables] = objId;
predQuery[preId] = variables;
}
else {
alert(predQuery[preId]);
var predIndex = predQuery[preId];
queryPreds[predIndex] = preId;
queryObjs[predIndex] = objId;
}
Run Code Online (Sandbox Code Playgroud)
如果需要,我可以添加更多代码.
当我尝试使用下面的代码实现自动完成时,我得到一个错误说明:
.data("autocomplete") is undefined
Run Code Online (Sandbox Code Playgroud)
然而,如果我从最后删除.data()方法它工作正常(只是没有.data()提供的可自定义图形).谁能告诉我出了什么问题?
$("input#testInput").bind("autocompleteselect", function (event, ui) {
}).autocomplete({
appendTo: "#autoCompList",
source: function (request, response) {
$.ajax({
url: JSONP CALL URL
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function (data) {
response($.map(data.data, function (item) {
fbPageJson = item;
return {
label: item.name,
image: item.picture,
json: item,
}
}));
},
});
}
}).data("autocomplete")._renderItem = function (ul, item) {
return $("<li></li>").data("item.autocomplete", item).append("<a><img src='" + item.image + "' alt='no photo'/></a>" + item.label).appendTo(ul);
};
Run Code Online (Sandbox Code Playgroud)