没有一个特定的词汇来表示一个元素上的多个点符号吗?

huangapple go评论61阅读模式
英文:

Is there a specific word for multiple dot notations on an element?

问题

使用多个点符号表示法访问变量时,你会如何称呼它?

例如:e.target.style.textDecoration

我已尝试在Google上搜索答案,但不确定如何准确表达这个问题。此外,我想熟悉如何在Stack Overflow 上提问。

英文:

What would you call using multiple dot notation's to a variable?

i.e: e.target.style.textDecoration

I've tried to Google search for an answer but not sure exactly how to word the question. Plus I would like to get familiar with asking questions on stackoverflow.

答案1

得分: 3

Chaining(链式调用)这是JavaScript中的一种技术,指的是在一行代码中连续调用对象上的多个方法。每个方法调用的结果被用作下一个方法调用的对象,依此类推,形成一系列方法调用的链条。

字面上来说,你正在创建一系列的调用!

使用链式调用可以使代码更加简洁,更容易阅读,因为它避免了创建临时变量来存储中间结果。

这里有一篇完整的文章介绍它

对于你的示例:

e.target.style.textDecoration

变量e是一个对象,target是该对象的属性,也是一个对象。然后,stylee.target的属性,依此类推。

方法链

方法链是一种简化和美化代码的编程策略。它是一种在同一对象的方法上调用另一个方法的机制。

这也可以用于方法,如果方法的返回类型是一个对象

例如:

document.querySelector('.divClass').click();

还有一种新的操作,你可以查看一下。它叫做可选链,它检查父对象不为空。如果父对象为空或非对象,就不会发生错误。
MDN 文档

英文:

Chaining

It's called Chaining.

In JavaScript, chaining refers to the technique of calling multiple methods on an object one after the other, in a single line of code. The result of one method call is used as the object on which the next method call is made, and so on, creating a chain of method calls.

Literally you are creating chains of calls!

Chaining can make code more concise and easier to read, as it avoids the need to create temporary variables to store intermediate results.

Here is a full article about it

For your Example:

e.target.style.textDecoration

Variable e is an object and target property of that is an object too. then style is a property of e.target and so on.

Method Chaining

> Method Chaining is a programming strategy that simplifies and embellishes your code. It is a mechanism of calling a method on another method of the same object.

This can be done for methods, too. If the method return type be an object.

For instance:

documet.querySelector('.divClass').click();

Also there is A new operation you can check it out. It's called Optional Chaining
It checks parent not to be null. So no error will be issued if parent is null or non-object.
MDN Documentation

huangapple
  • 本文由 发表于 2023年3月10日 01:58:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定