Home > Article > Web Front-end > How to use the with statement in js_javascript skills
1) Brief description
The with statement can be conveniently used to reference existing attributes in a specific object, but it cannot be used to add attributes to the object. To create new properties on an object, you must explicitly reference the object.
2) Syntax format
with(object instance)
{
’ s ‐ ‐ ‑ ‑ ‑ ‑ According to the previous writing method, the properties or methods of an object are obtained through: object.property or object.method. It is a bit troublesome to obtain the properties and methods of the object respectively. After learning the with statement, you can use something like the following To achieve this:
with(objInstance)
{
var str = attribute 1;
.....
} eliminates the trouble of writing the object name multiple times.
3) Example
The code execution effect is as follows:
Name: kobe bryant
Age: 28
Gender: boy