A case study of with function usage in JavaScript

Source: Internet
Author: User
Tags cos sin

This example describes the use of with functions in JavaScript. Share to everyone for your reference. The specific analysis is as follows:

The WITH function in JavaScript, known as the With statement, can be conveniently used to refer to an existing property in a particular object, but cannot be used to add properties to an object, and you must explicitly reference the object to create a new property.

With function to set the default object for the statement.

With (object)

Statements

Parameters:

Object

The new default object.

Statements

One or more statements, object is the default object for the statement.

Description

The With statement is typically used to shorten the amount of code that must be written in a particular case. In the following example, note the repeated use of Math:

The code is as follows:

x = Math.Cos (3 * Math.PI) + math.sin (math.ln10) y = Math.tan (* math.e)

When you use the WITH statement, the code becomes shorter and easier to read:

The code is as follows:

With (Math) {x = cos (3 * PI) + sin (LN10) y = tan (E)}

Application code Example:

?

1 2 3 4 5 6 <script> with (document) write (' filesize= ' +filesize), title= ' I title ', bgcolor= ' teal '; </script>

I hope this article will help you with your JavaScript programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.