Home > Web Front-end > JS Tutorial > Introduction to the basics of javascript Suitable for novices to learn_Basic knowledge

Introduction to the basics of javascript Suitable for novices to learn_Basic knowledge

WBOY
Release: 2016-05-16 18:46:56
Original
1183 people have browsed it

1. A brief introduction to JavaScript
Ø Most of the JavaScript mentioned on the Internet or in books refers to client-side JavaScript.
Ø JavaScript is a lightweight, interpreted, object-oriented programming language.
Ø JavaScript features
1) Control the appearance and content of the document
2) Control the browser
3) Interaction with HTML forms
4) Interaction with users
5) Use Cookie reads and writes user status
6) Others
2. Lexical structure
2.1. Character set
JavaScript programs are written using the Unicode character set.
2.2. Case Sensitive
JavaScript is a case-sensitive language.
2.3. Comments
//: //Any text on the following line is commented
/**/: in /**/Any text between is commented
2.4. Direct quantity
Direct quantity: data value that appears directly in the program
12 //Number 1.2 //Number "hello world" //String 'Hi' //String true //Boolean value false //Boolean value/JavaScript /gi //Regular expression null //Empty object { x:1, y:2 } //Object initializer [1,2,3,4,5] //Array initializer 2.5. Identifier
identification Fu is actually a name. In JAVASCRIPT, identifiers are used to name variables, functions, or labels used for certain loops in
JAVASCRIPT code.
Identifier naming rules, the first character must be a letter, underscore or dollar character, followed by letters, numbers, underscore or dollar character. Numbers are not allowed as the first character so that JAVASCRIPT can easily distinguish identifiers from numbers.
2.6. Reserved words
break
do
if
switch
typeof
case
else
in
this
var
catch
false
instanceof
throw
void
continue
finally
new
true
while
default
for
null
try
with
delete
function
return
3. Data types and values
3.1. Numbers
In JavaScript, numbers are not divided into integer types and floating point Type type, all numbers are composed of
floating point type. JavaScript uses the 64-bit floating point format defined by the IEEE754 standard to represent numbers. It can represent a maximum value of ±1.7976931348623157 x 10308 and a minimum value of ±5 x 10 -324
3.2. String
In JavaScript, string It is a sequence
consisting of Unicode characters, numbers, punctuation marks, etc., and you can use ' or " to represent a string.
3.3. Boolean
In JavaScript, the only Boolean types are true and false Two values.
3.4. Function
In JavaScript, a function is regarded as a data type. For example: var square = function(x) { return x*x; } 3.5. Object
3.6. . Array
3.7. null
The keyword null of JAVASCRIPT is a special value, which means "no value". Null is often regarded as a special value of the object type, which means the value of "no object". Null is a unique value that is different from all other values. If the value of a variable is null, then you know that its value is not a valid object, array, number, string, or Boolean value. . undefined
Undefined will be generated in the following situations:
Ø An undeclared variable is used
Ø A variable that has been declared but has not been assigned a value is used
Ø An object is used Property that does not exist


运行结果:
undefined
undefined
'test2' 未定义
3.9. Date对象
3.10. 正则表达式
3.11. Error对象
3.12. 基本数据类型的包装对象
4. 变量
4.1. JavaScript变量特点
在JavaScript中,变量有以下特点:
Ø 可以调用没有经过声明变量,例如:i=1;alert(i);
Ø 变量可以存储任何数据类型的值,例如:i=1;i='ddxkj';
4.2. 声明变量
提倡使用var关键字显式声明变量,例如:var i = 100;如果你没有显式声
明一个变量,JAVASCRIPT会帮你隐式声明它。如果使用var显式定义了一个变量,但没有赋值前,它了初始值是undefined
由var声明的变量是永久性的,也就是说,用delete运算符删除这些变量时会引发错误。
4.2.1. 重复的声明和遗漏的声明
使用var语句多次声明同一个变量不仅是合法的,而且也不会造成任何错误。
如果尝试读一个未声明的变量的值,JAVASCRIPT会生成一个错误。如果尝试给一个未
用var声明的变量赋值时,JAVASCRIPT会隐式声明该变量。但是要注意,隐式声明的变量总是被创建为全局变量,即使该变量只在一个函数体内使用。例如:

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

运行结果:
ddddd
ddddd
aaa
4.3. 变量的作用域
一个变量的作用域是程序中定义这个变量的区域。全局变量的作用域是全局性的,即在JAVASCRIPT代码中,它处处可以被调用。而在函数之内声明的变量和函数的参数,就只在函数体内有定义。它们是局部变量,作用域是局部的。
在函数体内部,局部变量使用的优先级高于同名的全局变量。当在函数体内部,定义了一个和全局变量同名的局部变量,那么全局变量就会被隐藏。所以要尽量避免这样一种情况,如果函数使用的是全局变量,而不是局部变量,那就有可能改变程序其它函数所使用该全部变量的值,而产生一些难于发现的问题。
4.3.1. 没有块级作用域
不同于JAVA或C语言,JavaScript是没有块级作用域。在函数体是,不论在什么位置定义了局部变量,局部变量在整个函数体内都可以被使用。例如:

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

运行结果:
0
1
2
3
4
5
6
7
8
9
10
100
当一个函数中,定义了一个和全局变量同名的局部变量时,往往结果不编写人员所想要的。例如:

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

运行结果:
undefined
local
4.3.2. 未声明的变量和未赋值的变量
Ø 未声明的变量:读取未声明的变量时,会引起运行时的错误,而使程序中止。
Ø 未赋值的变量:读取未赋值的变量时,将会得到一个缺省值,即undefined。

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

运行结果:
'i' 未定义
undefined
4.4. 基本类型和引用类型
基本类型:数值、布尔值、null、未定义的值
引用类型:数组、对象、函数
4.5. 变量特殊运算符
1) in运算符
in运算符要求其左边的运算数是一个字符串,或以可转换为字符串,右边的运算数是一个对象或数组。如果该运算符左边的值是其右边对象的一个属性名,它返回true,否则为false。

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

运行结果:
true
true
false
true
2) instanceof运算符
instanceof运算符要求其左边运算数是一个对象,右边是一个类,当对象是类的实例时,返回true,否则返回false。

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

运行结果:
true
true
false
true
true
false
3) typeof运算符
typeof运算符是一元运算符,放在一个变量之前,这人运算数可以是
任何类型,返回一个字符串,说明运算数的类型。
Ø 数字:返回number
Ø 字符串:返回string
Ø 布尔型:返回boolean
Ø 对象、数组、null:返回object
Ø 未定义变量:返回undefined

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

运行结果:
number
string
boolean
object
object
object
undefined
4) delete运算符
delete运算符是个一元运算符,它可以删除对象的属性、数组或变量。如果删除成功,返回true,如果不能删除,返回false。但并非所以属性和变量都是可以删除的,某些内部核心属性和客户端属性不能删除,如果试图删除时会发生运行时的错误。用var关键字定义的变量也不能删除。如果delete删除一个不存在的属性时,返回true。

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

运行结果:
true
undefined
true
false
不能删除 '[number]'
true
'x' 未定义
5) void运算符
void是一元运算符,它可以出现在任何类型变量之前,有两个用处:
Ø 舍弃运算数的值,如函数,然后返回undefined
Ø 生成undefined

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

运行结果:
ddxkj
undefined
undefined
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template