JavaScript base Hardened 5--Object

Source: Internet
Author: User
Tags natural logarithm square root


2.1 What is an object


All JavaScript data can be treated as objects, and each object has its attributes (properties) and methods (methods).



An object's properties reflect certain properties of the object, such as the length of the string, the width of the image, the text in a text box (Textbox), and so on;



The object's methods can do something about the object, such as the form's "submit" (submit), the window's "scrolling" (scrolling), and so on.



For example, when applying for a variable:


var my_var= "Shiyanlou";


In fact, it creates a string object that has built-in properties My_var.length = 9



At the same time, there are several built-in methods for this string object, such as the My_var.touppercase () method to capitalize the character, and the My_var.charat (n) method to return the nth character.


2.2 Creating objects


JavaScript provides some common built-in objects (described later), but in some cases we need to customize the objects to achieve special, rich functionality.



For example, we create a "student" object and specify several properties and methods for it:


Student = new Object(); // Create the object "student"

Student.name = "Tom"; // object property name
Student.age = "19"; // object properties age

Student.study =function() { // object method learning
     Alert("studying");
};

Student.eat =function() { // object method eat
     Alert("eating");
}; 


To create a new object by using a function:


 
function student(name,age) {

    this.name = name;
    this.age = age;

    this.study = function() {
        alert("studying");
    };

    this.eat = function() {
        alert("eating");
    }
}


Then create an instance of the student object with new:


var New student (' Tom ', ' n '); 
var New student (' Jack ', ' 20 ');
2.3 Accessing objects ' properties and methods


The object's existence meaning is that in order to manipulate the data conveniently, we can easily access the object's properties or invoke the object's method through the symbol ".".



For example, we have just created a new Student1 object that can be used like this:


<script>
Var x = student1.name; // access the properties of the object
Var y = student1.age;

Document.write(x);
Document.write(y);

Student1.study(); // method of calling the object
</script>


When we need to revisit an object over and over again, we can use the WITH statement to simplify the operation without having to use "." repeatedly. Symbols, such as:


With (student1) {var x = name; var y= age;study (); Eat ();}
2.4 Common built-in objects


JavaScript provides us with some very useful built-in objects, so that we do not have to code each time to implement some common functions.



Now we give an example of String, Math, Array three common objects and their properties and methods.


2.4.1 String Object


An example of a string object was introduced at the outset, and whenever a string variable is created, it is equivalent to creating a string object.


(1) Properties of String


The object has only one property, length, that represents the number of characters in the string, including all the spaces and symbols:


var test_var = "I Love you!" ;d ocument. Write (Test_var.length);


The result is "11" because the string length is also counted as a symbol and a space:








(2) Method of String


There are 19 built-in methods for string objects, including features such as the display of strings in a page, font size, font color, character search, and character conversion, and the following are some common:


    • CharAt (N): Returns the single character of the nth bit of the string. (counting starting from 0)

    • charCodeAt (N): Returns the ASCII code for a single character of the nth bit of the string.

    • IndexOf (): Usage: string_1.indexof (string_2,n); Start the search from the nth bit of the string string_1, find the string_2, return the found location, or 1 if not found, where n can not be filled, and the default is to start with the No. 0 bit.

    • LastIndexOf (): Similar to IndexOf (), but looking from behind.

    • Split (' delimiter '): The string is separated by the specified delimiter, returning an array, for example: ' 1&2&345&678 '. Split (' & '); Returns the array: 1, 2,345,678.

    • SUBSTRING (N,M): Returns the substring of the original string from the n position to the M position.

    • SUBSTR (N,X): Returns the original string from the n position and the substring of length x.

    • toLowerCase (): Returns a string that turns all uppercase letters in the original string into lowercase.

    • toUpperCase (): Returns a string that turns all lowercase letters of the original string into uppercase.

2.4.2 Math Object


The "Math" object, which provides mathematical calculations of the data.


(1) Properties of Math


Several properties of math are some of the most commonly used values in math:


    • E: Return constant E (2.718281828 ...).

    • LN2: Returns the natural logarithm of 2 (ln 2).

    • LN10: Returns the natural logarithm of 10 (ln 10).

    • LOG2E: Returns the logarithm (LOG2E) of the base 2 E.

    • LOG10E: Returns the logarithm (log10e) of the base 10 E.

    • Pi: Returns π (3.1415926535 ...).

    • Sqrt1_2: Returns the square root of 1/2.

    • SQRT2: Returns the square root of 2.

(2) The Math method


The built-in methods of math are some of the mathematically common mathematical operations:


    • ABS (x): Returns the absolute value of x.
    • Round (x): Returns the value after X is rounded.
    • sqrt (x): Returns the square root of x.
    • Ceil (x): Returns the smallest integer greater than or equal to X.
    • Floor (x): Returns the largest integer less than or equal to X.
    • Sin (x): Returns the sine of x.
    • cos (x): Returns the cosine of x.
    • Tan (x): Returns the tangent of x.
    • ACOs (x): Returns the inverse cosine of x (the angle of the cosine equal to X), expressed in radians.
    • ASIN (X): Returns the inverse sine of x.
    • Atan (x): Returns the inverse tangent value of x.
    • EXP (x): Returns the x power of E (e^x).
    • Pow (n, m): Returns the M power (nm) of N.
    • Log (x): Returns the natural logarithm of x (ln x).
    • Max (A, B): Returns the larger number in a, B.
    • Min (A, B): Returns the smaller number of a, B.
    • Random (): Returns a random number greater than 0 and less than 1.
2.4.3 Array Object


An array object is a collection of objects that can have different types of objects. Each member object of an array has an "subscript" that represents its position in the array (counting from 0).



Array subscript notation is enclosed in square brackets, such as:


myarray[2]= "Hello"


Note: JavaScript has only one-dimensional arrays, and to use multidimensional arrays, use this virtual method:


var New Array (newarraynewarraynewarray (), ...);


In fact, this is a one-dimensional array, and each element inside is an array. When invoking this "two-dimensional array" element:


MYARRAY[2][3] = ...;
(1) The properties of the Array


Length: Returns the size of the array, which is the number of elements in the array. It equals the subscript of the last element in the array plus one.



Therefore, to add an element, you only need to:



Myarray[myarray.length] = ...;


####(2)Array method

- join("specify delimiter"): Returns a string that concatenates the array elements separated by a specified delimiter.

- toString() : Converts the array to a string and returns the result.

- reverse() : Reverses the array elements.

- slice(n,m) : Returns a subarray from the nth element of the array to the mth element.

- sort(SortFunction) : Sorts the elements of the array according to the specified SortFunction.

- concat(Array\_1, Array\_2) : Used to connect two or more arrays. 





JavaScript base Hardened 5--Object


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.