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

How to use the with statement in js_javascript skills

WBOY
Release: 2016-05-16 19:08:52
Original
1207 people have browsed it

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template