Javascript 左侧的可选链接

Ma *_*rez 17 javascript typescript

是否可以在 Javascript赋值的左侧使用可选的链接运算符=

const building = {}
building?.floor?.apartment?.number = 3; // Is this possible?
Run Code Online (Sandbox Code Playgroud)

jca*_*alz 9

这是不可能的,对不起。

为了规范答案:MDN 文档对此没有明确说明,但您可以在 GitHub 中阅读提案的自述文件以获取更多信息。它

尽管有一些用例,但不支持以下内容;请参阅问题 #18的讨论:

  • 可选的属性分配: a?.b = c

在链接的问题中是评论1

好的,这个线程中似乎有一个粗略的协议,不要在第一次迭代中进行写入案例。

2

我们也在 TC39 上讨论了这个问题,委员会似乎对添加这个功能不太感兴趣。

所以我想这不太可能很快发生。

希望有所帮助;祝你好运!


kry*_*sov 8

我正在对此进行研究,遗憾的是,正如其他评论者已经指出的那样,截至撰写本文时,在打字稿中似乎不可能通过可选链接进行分配,并且确实会产生解析器警告:

赋值表达式的左侧可能不是可选属性访问。

当尝试类似的事情时:

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 网站,它们对于测试此类内容很有用: