JavaScript statements, comments, data types, access object method

1, JavaScript code blocks

JavaScript can be combined in batches.

Code block starts with a left curly brace and ends with the closing brace.

Action code block is a sequence of statements executed collectively.

<body>

<h1>我的 Web 页面</h1>
<p id="myPar">我是一个段落。</p>
<div id="myDiv">我是一个div。</div>
<p>
<button type="button" onclick="myFunction()">点击这里</button>
</p>
<script>
function myFunction(){
	document.getElementById("myPar").innerHTML="你好世界!";
	document.getElementById("myDiv").innerHTML="你最近怎么样?";
}
</script>
<p>当您点击上面的按钮时,两个元素会改变。</p>

</body>

2, single-line comments begin with //.

// 输出标题:
document.getElementById("myH1").innerHTML="欢迎来到我的主页";
// 输出段落:
document.getElementById("myP").innerHTML="这是我的第一个段落。";

3, JavaScript multi-line comments

多行注释以 /* 开始,以 */ 结尾。 

/*
下面的这些代码会输出
一个标题和一个段落
并将代表主页的开始
*/
document.getElementById("myH1").innerHTML="欢迎来到我的主页";
document.getElementById("myP").innerHTML="这是我的第一个段落。";

4, JavaScript variables

<script>
var x=5;
var y=6;
var z=x+y;
document.write(x + "<br>");
document.write(y + "<br>");
document.write(z + "<br>");
</script>

5, variable - naming rules

变量必须以字母开头
变量也能以 $ 和 _ 符号开头(不过我们不推荐这么做)
变量名称对大小写敏感(y 和 Y 是不同的变量)

6, a statement, a number of variables

You can declare many variables in a statement. Var statement to the beginning, are separated by commas variable to:
var = LastName "Doe", Age = 30, Job = "Carpenter";

Acknowledgment may span multiple lines:
var = LastName "Doe",
Age = 30,
Job = "Carpenter";

A plurality of values may not assign the same statement declared:
var X, Y, Z =. 1;

x, y is undefined, z is 1.

7、Value = undefined

In a computer program, often declare variables without value. Not used to declare a variable value, and its value is actually undefined.

After executed the following statement, the value of the variable carname will be undefined:
var carname;

8, JavaScript Data Types

值类型(基本类型):字符串(String)、数字(Number)、布尔(Boolean)、对空(Null)、未定义(Undefined)、Symbol。


引用数据类型:对象(Object)、数组(Array)、函数(Function)。

9, JavaScript has a dynamic type

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

var x;               // x 为 undefined
var x = 5;           // 现在 x 为数字
var x = "John";      // 现在 x 为字符串

10, JavaScript string

The character string is stored (such as "Bill Gates") variables.

It may be any text string in quotation marks. You can use single or double quotes:
Examples

var carname="Volvo XC60";
var carname='Volvo XC60';

11, JavaScript digital

JavaScript is only one digital type. With decimal number can also be without:
Example

var x1=34.00;      //使用小数点来写
var x2=34;         //不使用小数点来写

12, JavaScript Boolean

Boolean (logic) can have only two values: true or false.
to true X = var;
var Y = to false;

13, JavaScript arrays

The following code creates an array of cars named:
var = new new cars the Array ();
cars [0] = "Saab";
cars [. 1] = "Volvo";
cars [2] = "the BMW";

或者 (condensed array):
var cars=new Array(“Saab”,“Volvo”,“BMW”);

Or (literal array):
Example
var cars = [ "Saab", "Volvo", "BMW"];

14, JavaScript objects

Objects are separated by braces. Within brackets, the name and attribute of the object in the form of value pairs (name: value) is defined. Properties are separated by commas:
var Person = {FirstName: "John", LastName: "Doe", ID: 5566};

Examples of the above objects (Person) has three attributes: firstname, lastname, and id.

Spaces and wrap irrelevant. Statement may span multiple lines:
var Person = {
FirstName: "John",
LastName: "Doe",
ID: 5566
};

Object Properties There are two addressing modes:
Example
name = person.lastname;
name = Person [ "LastName"];

15、Undefined 和 Null

Undefined variable does not contain this value represents the value.

By setting the value of the variable to null to clear variable.
Examples of
cars = null;
Person = null;

16, declare a variable type

When you declare a new variable, you can use the keyword "new" to declare its type:
var = new new String carname;
var = new new the X-Number The;
var = the y-new new Boolean;
var cars = new new Array;
var the Person = new new Object;

17, JavaScript objects

JavaScript object is to have the properties and methods of data.

18, access to object properties

You can access the object properties in two ways:
Example. 1
person.lastName;
Example 2
Person [ "lastName"];

19, object methods

The method defines a function object, and stored as attributes of the object.

By adding object method calls () (as a function).

The person object instance visited the fullName () method:
Example
name = person.fullName ();

20, access to the object method

You can use the following syntax to create an object method:
methodName: function () {} code Lines

You can use the following syntax to access object methods:
objectName.methodName ()

Typically the fullName () method as a person object, fullName as an attribute.

There are several ways to create, use and modify the JavaScript objects.

Guess you like

Origin blog.csdn.net/weixin_43731793/article/details/92112388