JavaScript's personal learning is a handy note (i)

Source: Internet
Author: User
Tags access properties button type

Introduction to JavaScript

To learn the people can go to W3school http://www.w3school.com.cn/b.asp

JavaScript is the most popular programming language in the world.

The language can be used in HTML and the Web, and is more widely used in servers, PCs, laptops, tablets and smartphones.

Personal sense JavaScript is also particularly similar to OC, most of which is the same as OC syntax

JavaScript: Writing to an HTML output instance
document.write ("document.write ("<p>this is a paragraph</p>");

JavaScript: Example of reacting to events
<button type= "button" onclick= "alert (' welcome! ')" > Click here </button>

JavaScript uses

HTML the script in must be between <script> and </script> tags.

The script can be placed in the <body> and

JavaScript output

If you want to access an HTML element from JavaScript, you can use the document.getElementById (ID) method.

Use the ID property to identify the HTML element:

JavaScript statements

JavaScript is case sensitive.

JavaScript is sensitive to capitalization.

When writing JavaScript statements, be aware of whether to turn off the case toggle key.

The function getElementById is different from the getElementById.

Similarly, variable myvariable and myvariable are different.

JavaScript comments

Consistent with C language:///**/

JavaScript variables

Like algebra, JavaScript variables can be used to hold values (such as x=2) and expressions (such as z=x+y).

Variables can use short names (such as x and y), or they can use better descriptive names (such as age, Sum, totalvolume).

    • Variables must start with a letter
    • Variables can also start with the $ and _ symbols (although we do not recommend this)
    • Variable names are case sensitive (Y and y are different variables)
var pi=3.14; var name= "Bill Gates"; var answer= ' Yes I am! ';

JavaScript Data types

JavaScript has a dynamic type. This means that the same variables can be used for different types:

Object properties are addressed in two ways:

Instance
Name=person.lastname;name=person["LastName"];

When you declare a new variable, you can use the keyword "new" to declare its type: var carname=new String;
var x=      new number ; var y=      new  Boolean; var cars=   new  Array; var New Object;

JavaScript variables are objects. When you declare a variable, a new object is created.

JavaScript Object access properties of an object

The syntax for accessing object properties is:

objectName. PropertyName

The length property of the string object to find the lengths of the strings

The toUpperCase () method of the String object to convert the text to uppercase

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: instance

person=New  Object ();p erson.firstname= "Bill";p erson.lastname= "Gates";p erson.age =56;p erson.eyecolor= "Blue";

Using the Object Builder

This example uses a function to construct an object:

Instance
function Person (Firstname,lastname,age,eyecolor) {this. firstname=FirstName; this. lastname=LastName; this. age= age; this. eyecolor=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", "page", "Blue"); var mymother=New person ("Steve", "Jobs", "green");

JavaScript functions

A function is a block of code wrapped in curly braces, preceded by a keyword function

You can send any number of arguments separated by commas (,):

MyFunction (argument1,argument2)

When you declare a function, declare the argument as a variable:

function myFunction (VAR1,VAR2) {This is the code to execute}

Variables and parameters must appear in a consistent order. The first variable is the given value of the first parameter passed, and so on.

Assigning a value to an undeclared JavaScript variable

If you assign a value to a variable that has not been declared, the variable is automatically declared as a global variable.

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.

Comparison operators

Comparison operators are used in logical statements to determine whether a variable or value is equal. Other same C

Operator

Describe

Example

===

Congruent (value and type)

X===5 is true;x=== "5" is false

For/in Cycle with OC

The JavaScript for/in statement loops through the properties of the object:

Instance
var person={fname: "John", lname: "Doe", age:25 for on person )  {  txt =txt + person[x];  }


Break statement is used to jump out of a loop.

Continue used to skip an iteration in the loop.

JavaScript errors-Throw, Try, and Catch

The try statement tests the code block for errors.

The catch statement handles the error.

The throw statement creates a custom error.

JavaScript Testing and capturing

The try statement allows us to define a block of code that performs error testing at execution time.

The catch statement allows us to define a block of code that executes when a try block of code has an error.

JavaScript statements try and catch are paired occurrences

Throw statement

The throw statement allows us to create custom errors.

The correct technical term is: Create or throw an exception (exception).

If you use throw with try and catch, you can control the flow and generate custom error messages.

Try  { varX=document.getelementbyid ("Demo"). Value; if(x== "")Throw"Empty"; if(IsNaN (x))Throw"Not a number"; if(X&GT;10)Throw"Too High"; if(x<5)Throw"Too Low"; }Catch(err) {varY=document.getelementbyid ("mess"); Y.innerhtml= "Error:" + Err + "."; }

JavaScript form validation Cue box function
Alert (Alerttxt)

More properties, functions, methods, etc., see the website brochure

JavaScript's personal learning is a handy note (i)

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.