Home  >  Article  >  Web Front-end  >  How to use the with statement in js_javascript skills

How to use the with statement in js_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:08:521205browse

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn