Ma *_*rez 17 javascript typescript
是否可以在 Javascript赋值的左侧使用可选的链接运算符=?
const building = {}
building?.floor?.apartment?.number = 3; // Is this possible?
Run Code Online (Sandbox Code Playgroud)
我正在对此进行研究,遗憾的是,正如其他评论者已经指出的那样,截至撰写本文时,在打字稿中似乎不可能通过可选链接进行分配,并且确实会产生解析器警告:
赋值表达式的左侧可能不是可选属性访问。
当尝试类似的事情时:
class A{
b?: string;
}
var a = new A();
a?.b = "foo";
Run Code Online (Sandbox Code Playgroud)
但由于可选赋值有时很有用,因此仍然有使用单行 if 查询的老式方法,如下所示:
class A{
b?: string;
}
try{a.b = "foo0"} // throws TypeError
catch(e){console.log(e.toString())} // a is undefined
if(a) a.b = "foo1"; // skips
console.log(a); // a is undefined
var a: any;
if(a) a.b = "foo2"; // skips
console.log(a); // a is undefined
a = null;
if(a) a.b = "foo3"; // skips
console.log(a); // a is null
a = undefined;
if(a) a.b = "foo4"; // skips
console.log(a); // a is undefined
a = new A();
if(a) a.b = "foo5"; // runs
console.log(a); // a is A: {"b": "foo5"}
if(null) console.log("bar0"); // skips
if(undefined) console.log("bar1"); // skips
if({}) console.log("bar2"); // runs
if({something: "there"}) console.log("bar3"); // runs
if([]) console.log("bar4"); // runs
if(["not empty"]) console.log("bar5"); // runs
Run Code Online (Sandbox Code Playgroud)
为了演示实际上可行的示例,这里有一个 kotlin 代码片段:
class A{
var b: B? = null;
var title: String? = null;
override fun toString():String = "Instance of class A with title: ${this.title} and b of value: ${this.b.toString()}";
}
class B{
var title: String? = null;
override fun toString():String = "Instance of class B with title: ${this.title}";
}
fun main() {
var a:A? = null;
println(a); // null
try{a!!.title = "foo0"} catch(e:Exception){println(e)} // NPE
a?.title = "foo1";
println(a); // null
a = A();
println(a); // Instance of class A with title: null and b of value: null
a?.title = "foo2";
println(a); // Instance of class A with title: foo2 and b of value: null
try{a!!.b!!.title = "bar0"} catch(e:Exception){println(e)} // NPE
a?.b?.title = "bar1";
println(a); // Instance of class A with title: foo2 and b of value: null
a?.b = B();
println(a); // Instance of class A with title: foo2 and b of value: Instance of class B with title: null
a?.b?.title = "bar2";
println(a); // Instance of class A with title: foo2 and b of value: Instance of class B with title: bar2
a?.b?.let{it.title = "bar3"}
println(a); // Instance of class A with title: foo2 and b of value: Instance of class B with title: bar3
}
Run Code Online (Sandbox Code Playgroud)
现在,我并不是说一种语言比另一种更好,而是说目的和理念需要不同的设计选择,并决定工具的最终形状,最终任何编程或脚本语言都是如此。同时也有点恼火的是我无法为打字稿中的可选链接赋值。
编辑:我使用了这些 Playground 网站,它们对于测试此类内容很有用:
| 归档时间: |
|
| 查看次数: |
2417 次 |
| 最近记录: |