Web Development Technology--javascript Object 1 (numeric, string, date)

Source: Internet
Author: User

JavaScript all things in are objects: strings, numbers, arrays, functions ...

In addition, JavaScript allows custom objects to be customized.

JavaScript 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 .

Accessing the properties of an object

A property is a value associated with an object.

The syntax for accessing object properties is:

objectName. PropertyName

This example uses the Length property of the string object to get the lengths of the strings:

var message= "Hello world!";
var x=message.length;

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

Methods for accessing objects

Method is an action that can be performed on an object.

You can invoke a method by using the following syntax:

objectName. MethodName ()

This example uses the toUpperCase () method of the String object to convert the text to uppercase:

var message= "Hello world!";
var x=message.toUpperCase();

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

HELLO world!
Creating JavaScript Objects

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

There are two different ways to create a new object:

1. Defining and creating an instance of an object

2. Use a function to define an object, and then create a new object instance

Create a direct instance

This example creates a new instance of the object and adds four properties to it:

person = new Object (); person.firstname = "Bill"; person.lastname = "Gates"; person.age = 56; person.eyecolor = "blue"; Alternative syntax (using object literals):

person = {firstname: "John", lastname: "Doe", age: 50, eyecolor: "blue"}; 
Person=new Object ();
Person.firstname= "Bill";
Person.lastname= "Gates";
Person.eyecolor= "Blue";

Alternative syntax (using object literals):

Person={firstname: "John", LastName: "Doe", Age:50,eyecolor: "Blue"};
Using the Object Builder

This example uses a function to construct an object:

function person (firstname,lastname,age,eyecolor)

Creating an instance of a JavaScript object

Once you have the object constructor, you can create a new object instance, just like this:

var myfather=new person ("Bill", "Gates", "a", "Blue");
var mymother=new person ("Steve", "Jobs", "green");
Add attributes to JavaScript objects

You can add a new property to an existing object by assigning a value to the object:

Suppose Personobj already exists-you can add these new properties to it: FirstName, LastName, Age, and Eyecolor:

Person.firstname= "Bill";
Person.lastname= "Gates";
Person.eyecolor= "Blue";


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

Add a method to a JavaScript object

method is simply a function attached to an object.

Methods for defining objects inside the constructor function:

function person(firstname,lastname,age,eyecolor){
    function changeName(name){this.lastname=name;}

The value of the ChangeName () function name is assigned to the LastName property of the person.

Now you can try it:

Mymother.changename ("Ballmer");
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, not class-based.

JavaScript for...in Loops

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

For (variable in object)
The code to execute

Note:code blocks in the for...in loop are executed once for each property.


Iterate through the properties of an object:

var person={fname: "Bill", lname: "Gates", age:56};

for (x in person)
Txt=txt + person[x];
JavaScript Number Object

· JS Object

· JS string

JavaScript has only one numeric type.

You can use it or you can write a number without using a decimal point.

JavaScript numbers

JavaScript numbers can be used or written without using a decimal point:

var pi=3.14; Use decimal point
var x=34; Do not use decimal points

The maximum or minimum number can be written by means of the scientific (exponential) notation:

var y=123e5; 12300000
var z=123e-5; 0.00123
All JavaScript numbers are 64-bit

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

All the numbers in JavaScript are stored as 64 bits (8 bits) of the root 10, floating-point numbers.


Integers (not using decimal or exponential notation) are up to 15 bits.

The maximum number of decimal digits is 17, but the floating-point operation is not always 100% accurate:

var x=0.2+0.1;
Octal and hexadecimal

If the prefix is 0, JavaScript interprets numeric constants as octal numbers, and if the prefixes are 0 and "X", they are interpreted as hexadecimal numbers.

var y=0377;
var Z=0xff;

tip: never write 0 in front of the number unless you need to make an octal conversion.

Numeric properties and Method properties:



· Negative infinitive

· POSITIVE Infinitive

· NaN

· Prototype

· Constructor


· Toexponential ()

· ToFixed ()

· Toprecision ()

· ToString ()

· ValueOf ()

Full Number Object reference manual

For a complete reference to all the properties and methods available for the number object, please visit our Number object reference manual.

The reference manual contains a description and an instance of each property and method.

3 JavaScript String Object

· JS number

· JS Date

String object is used to manipulate existing block of characters.

JavaScript string (String) object instance

Calculate the length of a string

How to use the Length property to calculate the length of a string.

Add a style to a string

How to add a style to a string.

IndexOf () method

How to use IndexOf () to position the first occurrence of a specified character in a string.

Match () method

How to use Match () to find a specific character in a string and, if found, to return the character.

How to replace characters in a string-replace ()

How to use the Replace () method to replace characters with some characters in a string.

String Object

A string object is used to handle an existing block of characters.


The following example uses the Length property of a String object to calculate the length of a string.

var txt= "Hello world!"
document.write (txt.length)

The above code output is:


The following example uses the toUpperCase () method of a String object to convert a string to uppercase:

var txt= "Hello world!"
document.write (txt.toUpperCase())

The above code output is:

HELLO world!

3 JavaScript Date (date) object

· JS string

· JS Array

Date objects are used to process dates and times.

JavaScript Date (date) Object instance

Return date and time of day

How to use the date () method to get the day.

GetTime ()

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

setFullYear ()

How to use setFullYear () to set a specific date.

toUTCString ()

How to use toUTCString () to convert the date of the day (based on UTC) to a string.

GetDay ()

How to use GetDay () and an array to display the week, not just the numbers.

Show a clock

How to display a clock on a webpage.

Complete reference manual for Date objects

We provide a reference manual for the JavaScript Date object, which includes all the properties and methods available for date objects.

The manual contains a detailed description of each of the properties and methods and the relevant examples.

Define Date

The Date object is used to process dates and times.

You can define a Date object by using the New keyword. The following code defines a Date object named MyDate:

var mydate=new Date ()

notes: The Date object automatically uses the current day and time as its initial value.

Operation Date

By using a method for a Date object, we can easily manipulate the date.

In the following example, we set a specific date for the Date object (August 9, 2008):

var mydate=new Date ()

Mydate.setfullyear (2008,7,9)

Note: The parameter that represents the month is between 0 and 11. That is, if you want to set the month to August, the parameter should be 7.

In the following example, we set the Date object to a date after 5 days:

var mydate=new Date ()

Mydate.setdate (Mydate.getdate () +5)

Note: If you increase the number of days to change the month or year, the Date object will automatically complete the conversion.

Compare dates

A Date object can also be used to compare two dates.

The following code compares the current date with August 9, 2008:

var myDate=new Date();
var today = new Date();
if (myDate>today)

    alert("Today is before 9th August 2008");

    alert("Today is after 9th August 2008");

Web Development Technology--javascript Object 1 (numeric, string, date)

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.