Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the objects of Javascript?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you what Javascript objects, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

JavaScript object

Everything in JavaScript is an object: strings, numeric values, arrays, functions. Wait.

In addition, JavaScript allows custom objects.

JavaScript provides multiple built-in objects, such as String, Date, Array, and so on. Objects are just special data types with properties and methods.

A Boolean can be an object.

A digital type can be an object.

A string can also be an object.

The date is an object.

Mathematics and regular expressions are also objects.

An array is an object.

Even a function can be an object.

Http://www.iis7.com/b/plc/

An object is just a special kind of data. The object has Properties and methods.

Access the properties of the object:

Property is the value associated with the object.

The syntax for accessing object properties is: objectName (object name). (attribute name) propertyName

This example uses the length property of the String object to get the length of the string:

[var message= "Hello World!"

Var x=message.length

After the above code is executed, the value of x will be:

12]

Create a JavaScript object:

With JavaScript, you can define and create your own objects.

There are two different ways to create a new object:

Define and create an instance of the object.

Use functions to define objects, and then create new object instances.

Use the object constructor.

In JavaScript, this usually points to the function we are executing, or to the object to which the function belongs (runtime).

Add attributes to the JavaScript object:

You can add new properties to existing objects by assigning values to them.

Add a method to the JavaScript object:

A method is simply a function attached to an object.

::

JavaScript class:

JavaScript is an object-oriented language, but JavaScript does not use classes.

In JavaScript, classes are not created, and objects are not created through classes (as in other object-oriented languages).

JavaScript is based on prototype (prototype), not class.

JavaScript for...in loop:

The JavaScript for...in statement loops through the properties of the object.

Syntax:

For (variable (variable) in object)

{

The code executed...

}

Note: the code block in the for...in loop will be executed once for each attribute.

:::

JavaScript (Number) object

JavaScript has only one number type, and you can write numbers with or without decimal points.

JavaScript numbers can be written with or without decimal points.

Very large or very small numbers can be written by scientific (index "e") counting.

All JavaScript digits are 64 bits

JavaScript is not a typed language. Unlike many other programming languages, JavaScript does not define different types of numbers, such as integers, short, long, floating point, and so on.

Numbers are not divided into integer types and floating-point types, all numbers are made up of floating-point types. The 64-bit floating-point format defined by the IEEE754 standard is used to represent numbers, which can represent a maximum of ±1.7976931348623157 x 10308 and a minimum of ±5 x 10-324.

Precision:

Integers (without decimal or exponential counting) are up to 15 digits.

The maximum number of decimal places is 17, but floating-point operations are not always 100% accurate.

Octal and hexadecimal:

If the prefix is 0, JavaScript interprets the numeric constant as an octal number, and if the prefix is 0 and "x", as a hexadecimal number. Never write zeros in front of a number unless you need to do octal conversion.

By default, JavaScript numbers are displayed in decimal, but you can use the toString () method to output hexadecimal, octal, and binary.

Infinity (Infinity)

When the result of a number operation exceeds the upper limit of the number that can be represented by JavaScript (overflow), the result is a special infinity value, expressed as Infinity in JavaScript. Similarly, when the value of a negative number exceeds the range of negative numbers that JavaScript can represent, the result is negative infinity, which is represented by-Infinity in JavaScript. The behavior of infinity values is consistent with what we expect: based on their addition, subtraction, multiplication and division operations, the results are still infinity (and of course retain their plus and minus signs).

NaN-non-numeric value:

The NaN attribute is a special value that represents non-numeric values. This property is used to indicate that a value is not a number. You can set the Number object to this value to indicate that it is not a numeric value.

You can use the isNaN () global function to determine whether a value is a NaN value. Dividing by 0 is infinity, and infinity is a number, so false is returned.

A number can be a number or an object:

Numbers can be initialized with private data, such as x = 123

JavaScript digital object initialization data, var y = new Number.

Numeric attributes:

MAX_VALUE

MIN_VALUE

NEGATIVE_INFINITY

POSITIVE_INFINITY

NaN

Prototype

Constructor

Digital method:

ToExponential ()

ToFixed ()

ToPrecision ()

ToString ()

ValueOf ()

:::

JavaScript string (String) object

The String object is used to process existing character blocks.

A string is used to store a series of characters like "John Doe".

A string can use single or double quotation marks.

You can use the position (index) to access any character in the string, the index of the string starts from zero, so the first character of the string is [0], the second character is [1], and so on.

You can use escape characters (\) in strings using quotation marks.

String (String) uses the length attribute length to calculate the length of the string.

The string uses indexOf () to locate the first occurrence of a specified character in the string, and returns-1 if the corresponding character function is not found.

The lastIndexOf () method starts looking for the location where the string appears at the end of the string.

Content matching:

The match () function is used to find a specific character in a string and returns that character if found.

Replace content:

The replace () method replaces some characters in a string with others.

String case conversion:

String case conversion uses functions (uppercase) toUpperCase () and (lowercase) toLowerCase ().

Convert a string to an array:

Strings are converted to arrays using the split () function.

Special characters:

In Javascript, you can use backslashes (\) to insert special symbols, such as apostrophes, quotation marks, and so on. In JavaScript, single or double quotation marks are used for the beginning and end of a string.

The backslash escapes special characters:

\ 'single quotation marks

\ "double quotation marks

\\ diagonal bar

\ nWrap

\ r enter

\ ttab

\ b spaces

\ f Page change

String properties and methods:

Attributes:

Length

Prototype

Constructor

Methods:

CharAt ()

CharCodeAt ()

Concat ()

FromCharCode ()

IndexOf ()

LastIndexOf ()

Match ()

Replace ()

Search ()

Slice ()

Split ()

Substr ()

Substring ()

ToLowerCase ()

ToUpperCase ()

ValueOf ()

:::

JavaScript Date (date) object

The date object is used to process the date and time.

Date (), use Date () to get the date of the day.

GetFullYear (), using getFullYear () to get the year.

GetTime (), getTime () returns the number of milliseconds since January 1, 1970.

SetFullYear (), using setFullYear () to set the specific date. Remember that the number of JavaScript months is from 0 to 11. October is November.

ToUTCString (), which uses toUTCString () to convert the date of the day (based on UTC) to a string.

GetDay (), which uses getDay () and an array to display days of the week, not just numbers.

Display a clock, which displays a clock on the web page.

GetHours () to get the current number of hours.

GetMinutes () to get the current score.

GetSeconds () to get the current number of seconds.

The Date object is used to handle the date and time, and the Date object can be defined by the new keyword.

:::

JavaScript Array (array) object

The purpose of an array object is to use separate variable names to store a series of values.

What is an array?

Array objects use separate variable names to store a series of values.

If you have a set of data (for example, the name of the car), there are separate variables as follows:

Var car1= "Saab"

Var car2= "Volvo"

Var car3= "BMW"

However, what if you want to find a certain car? And not three, but 300? It will not be an easy task!

The best way is to use arrays.

An array can store all values with a variable name, and any value can be accessed with a variable name.

Each element in the array has its own ID so that it can be easily accessed.

There are three ways to create an array:

The following code defines an array object named myCars:

1: general method:

Var myCars=new Array ()

MyCars [0] = "Saab"

MyCars [1] = "Volvo"

MyCars [2] = "BMW"

2: concise way:

Var myCars=new Array ("Saab", "Volvo", "BMW")

3: literally:

Var myCars= ["Saab", "Volvo", "BMW"]

Access array:

You can access a particular element by specifying the array name and index number.

[0] is the first element of the array. [1] is the second element of the array, and so on.

You can have different objects in an array:

All JavaScript variables are objects. Array elements are objects. A function is an object.

Therefore, you can have different variable types in the array.

You can include object elements, functions, and arrays in an array:

MyArray [0] = Date.now

MyArray [1] = myFunction

MyArray [2] = myCars

Array methods and properties:

Use array object predefined properties and methods:

The number of elements in var x=myCars.length / / myCars.

The index value of the var y=myCars.indexOf ("Volvo") / / "Volvo" value.

The prototype is the JavaScript global constructor. It can build the properties and methods of a new Javascript object.

Concat (), merge the array.

Join (), the elements of the array make up the string output.

Shift (), delete the first element of the array.

Pop (), delete the last element of the array.

Unshift (), adding a new element at the beginning of the array.

Push (), adding a new element to the end of the array.

Reverse () reverses the order of the elements in the array.

Slice (), which selects elements from an array.

Sort (), array sort (in ascending alphabetical order).

ToString (), which converts the array to a string.

:::

JavaScript Boolean (Boolean) object

The Boolean (Boolean) object is used to convert non-Boolean values to Boolean values (true or false) and to check whether the Boolean object is true or false.

The Boolean object represents two values: "true" or "false".

The following code defines a Boolean object named myBoolean:

Var myBoolean=new Boolean ()

If the Boolean object has no initial value or its value is:

-0

Null

"

False

Undefined

NaN

Then the value of the object is false. Otherwise, its value is true (even when the variable value is the string "false").

:

JavaScript Math (arithmetic) object

The purpose of the Math object is to perform common arithmetic tasks.

Math.round (), rounded.

Random (), which uses random () to return a random number between 0 and 1.

Max (), which uses max () to return the larger of the two given numbers (before ECMASCript v3, this method had only two parameters).

Min (), which uses min () to return the smaller of the two given numbers (before ECMASCript v3, this method had only two parameters).

Math object:

The purpose of the Math object is to perform ordinary arithmetic tasks.

The Math object provides a variety of arithmetic value types and functions. Note: there is no need to define this object before using it.

Calculation value:

JavaScript provides 8 arithmetic values that can be accessed by Math objects, and how to use constants:

Math.E

Math.PI

Math.SQRT2

Math.SQRT1_2

Math.LN2

Math.LN10

Math.LOG2E

Math.LOG10E

Method of arithmetic:

In addition to the arithmetic values that can be accessed by the Math object, there are several functions (methods) that can be used.

The following example uses the round method of the Math object to round a number.

[xss_clean] (Math.round (4.7)

The output of the above code is: 5

The following example uses the random () method of the Math object to return a random number between 0 and 1:

[xss_clean] (Math.random ())

The output of the above code is: random 0.6287247786957237

The following example uses the floor () method and random () of the Math object to return a random number between 0 and 11:

[xss_clean] (Math.floor (Math.random () * 11))

The output of the above code is: random 5

:

JavaScript RegExp (regular expression) object, RegExp: is an abbreviation for regular expression (regular expression).

What is RegExp? :

A regular expression describes the pattern object of a character.

When you retrieve a text, you can use a pattern to describe what you want to retrieve. This is the mode of RegExp.

A simple pattern can be a single character.

More complex patterns include more characters and can be used for parsing, format checking, substitution, and so on.

You can specify the location in the string to retrieve, the type of characters to retrieve, and so on.

Syntax:

Var patt=new RegExp (pattern,modifiers)

Or a simpler way.

Var patt=/pattern/modifiers

The pattern describes an expression model.

The modifiers describes whether the retrieval is global, case-sensitive, and so on.

Note: when using constructors to create regular objects, you need regular character escape rules (preceded by a backslash\).

RegExp modifier:

Modifiers are used to perform case-insensitive and full-text searches.

The I-modifier is used to perform case-insensitive matches.

The g-modifier is used to perform a full-text search (instead of stopping the search as soon as the first one is found, but finding all matches).

Gi, full-text search and case-insensitive search.

The test (), test () method searches for the value specified by the string and returns true or false based on the result.

The exec (), exec () method retrieves the specified value in the string. The return value is the value found. If no match is found, null is returned.

These are all the contents of the article "what are the objects of Javascript?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report