`

JAVASCRIPT基础学习篇(5)--ECMAScript Basic1(EcmaScript 基本类型)

阅读更多

第一章 基本类型

一、The basic concepts of ECMAScript are the following:(最基本语法)
1、Everything is case-sensitiv(区分大小写)
2、Variables are loosely typed。(松散类型变量)
只需使用var关键字来定义变量如:var visible = true;
End-of-line semicolons are optional(行尾的分号是可选的即不一定有)
如以下均可:var test1 = “red” var test2 = “blue”;
3、Comments are the same as in Java, C, and Perl(注释与其它如java一样可使用//或/* */来注释。)
4、Braces indicate code blocks(括号表明代码块)

二、Variables
1、 变量声明:
var test = “hi”, age = 25;
声明两个变量test,age,javascript会自动创建一个String类型和一个int类型的变量。
变量不一定在声明的时候就初始化值,如:var test;即声明了一个变量test但未初始化。
变量可以赋以不同类型的值如:
var test = 'sfsf';
test = 40
以上是可以的,这都是因为JS的松散类型的优点所在。这些在JAVA中是不行的。

2、 变量名约束:
In terms of variables names, a name must follow two simple rules:
The first character must be a letter, an underscore (_), or a dollar sign ($).
All remaining characters may be underscores, dollar signs, or any alphanumeric characters.
All the following variable names are legal:
var test;
var $test;
var $1;
var _$te$t2;

3、变量名的写法
有三种写法如下:
Camel Notation:(第一个单词小写,以后每个单词首字母大写)
var myTestValue = 0, mySecondTestValue = “hi”;
Pascal Notation:(每个单词首字母大写)
var MyTestValue = 0, MySecondTestValue = “hi”;
Hungarian Type Notation:(每个单词首字母大写,但在最前面加上变量类型符)
var iMyTestValue = 0, sMySecondTestValue = “hi”;

4、变量在使用之前不声明的情况
如下:var test ="hello"
test2 = test + "world"
alert(test2)
结果为: hello world
注意的是这里javascript默认创建了test2变量,并且作为全局变量,所以使用的时候要注意,因为在程序中某一处改变了这个变量的值,则结果即将随之改变

三、关键字
以下关键字不能作为变量或方法名
break else new var
case finally return void
catch for switch while
continue function this with
default if throw
delete in try
do instanceof typeof

保留字
以下保留字不能作为变量或方法名,留作将来使用
abstract enum int short
boolean export interface static
byte extends long super
char final native synchronized
class float package throws
const goto private transient
debugger implements protected volatile
double import public

四、基本类型变量与引用类型变量(Primitive and Reference Values)
Primitive values are simple pieces of data that are stored on the stack, which is to say that their
value is stored directly in the location that the variable accesses.
基本类型变量存储在栈中,它的值直接存储在变量所指的空间
Reference values, on the other hand, are objects that are stored in the heap, meaning that the value stored in the variable location is a pointer to a location in memory where the object is stored.
引用类型存储在堆中,它通过一个对象指向它
ECMAScript primitive types(基本类型变量有):Undefined, Null, Boolean, Number, or String
In many languages, strings are considered a reference type and not a primitive type because a string can vary in length. ECMAScript breaks from this tradition.
在许多语言中,String类型因为其长度可以改变而被当作引用类型变量,但在ECMAScript中打破了传统

五、typeof操作符
用于判断变量的类型
The typeof operator takes one parameter: the variable or value to check.
typeof(50) //parameter is value
var str = "sfsfsf"
typeof(str) //parameter is variable
typeof可能返回的值
“undefined” if the variable is of the Undefined type.
(当一个变量只声明,但没有赋值时,那个这个变量默认的值为undefined,使用typeof将返回它的类型为undefined
“boolean” if the variable is of the Boolean type.
“number” if the variable is of the Number type.
“string” if the variable is of the String type.
“object” if the variable is of a reference type or of the Null type.

六、The Undefined type
Note that a variable having the value of undefined is different from a value being undefined
需要注意的是一个变量的值未定义(即变量没有被初始化)不能说成是变量未定义
如下:
var temp;
alert(typeof temp) //返回undefined,但变量temp已经声明
alert(typeof temp2); //也返回undefined,但变量temp2从未声明
注意的是如果这样写:
alert(temp) //返回undefined
alert(temp2) //报错提示:temp2未定义,未定义的变量不能参与运算。
所以,typeof返回undefined时表示变量没有初始化或变量不存在
当一个函数没有明确的返回值时,将返回undefined
function testFunc() {
//leave the function blank
}
alert(testFunc() == undefined); //outputs “true”
alert(typeof testFunc()) //outputs "undefined"

七、The Null type
the Null type, has only the special value null
alert(null == undefined); //outputs “true”
Even though the values are both true, they are considered to have different meanings. Whereas
undefined is the value assigned when a variable is declared and not initialized, null is the value used
to represent an object that doesn’t exist
返回为true,但null和undefined的意义不同

八、The Boolean type
It has two values, true and false。
Even though false isn’t equal to 0, 0 is converted to false
布尔值为true或false,但js中把非0当作true,0当作false

九、The Number type
定义整型:var iNum = 55;
定义8进制整型: var iNum = 070;
For an octal literal,the first digit must be a zero (0), and the following digits can be any octal digit (0 through 7)
定义十六进制:var iNum = 0x1f
To create a hexadecimal literal, the first digit must be a zero (0) followed by the letter x, followed by any
number of hexadecimal digits (0-9 and A-F).
定义浮点型
var s = 4.5;
The interesting thing about this form of floating-point literal is that it is actually stored as a string until it’s needed for calculation.
实际上浮点型存储的是字符串型,直到运算的时候
var fNum = 3.125e7; 表赤的中3.125 * 107

INFINITY
finite有限的,infinite无限的、无穷的
When a calculation results in a number greater than Number.MAX_VALUE, it is assigned a value of
Number.POSITIVE_INFINITY, meaning that it has no numeric value anymore. Likewise a calculation
that results in a number less than Number.MIN_VALUE is assigned a value of Number.NEGATIVE_
INFINITY, which also has no numeric value. If a calculation returns an infinite value, the result cannot
be used in any further calculations.
Infinity. Number.POSITIVE_INFINITY has a value of Infinity, whereas Number.NEGATIVE_INFINITY has a value of –Infinity.
当一个数字大于1.7976931348623157E+10308时正无穷,当一个数字小于-1.7976931348623157E+10308时负无穷。
任何数除以无穷大为0,任何数乘以无穷大为无穷大。
The isFinite() method can be called on any number to ensure that the number isn’t infinite.
var iResult = iNum* some_really_large_number;
if (isFinite(iResult)) {
alert(“Number is finite.”);
} else {
alert(“Number is infinite.”);
}

NaN :Not a Number非数值
alert(NaN == NaN); //outputs “false”
alert(isNaN(“blue”)); //outputs “true”
alert(isNaN(“123”)); //outputs “false”

十、The String type
The String type is unique in that it is the only primitive type that doesn’t have a definite size.
是基本类型中唯一一种具有非固定长度的类型
String literals are specified by using either double quotes (“) or single quotes (‘).
字符串类型可用双引号或单引号包括起来,这与在java等语言中是不同的。
Literal Meaning
\n Newline
\t Tab
\b Backspace
\r Carriage return
\f Formfeed
\\ Backslash
\’ Single quote
\” Double quote
\0nnn A character represented by octal code nnn (where n is an octal digit 0-7)
\xnn A character represented by hexadecimal code nn (where n is a hexadecimal digit 0-F)
\unnnn A Unicode character represented by hexadecimal code nnnn (where n is a hexadecimal digit 0-F)

十一、Converting to a string
toString()方法:
布尔类型toString()
var bFound = false;
alert(bFound.toString()); //outputs “false”
数值类型的toString(),可带进制参数值
var iNum1 = 10;
var fNum2 = 10.0;
alert(iNum1.toString()); //outputs “10”
alert(fNum2.toString()); //outputs “10”

var iNum = 10;
alert(iNum1.toString(2)); //outputs “1010”
alert(iNum1.toString(8)); //outputs “12”
alert(iNum1.toString(16)); //outputs “A”

十二、Converting to a number
parseInt()
var iNum1 = parseInt(“1234blue”); //returns 1234
var iNum2 = parseInt(“0xA”); //returns 10
var iNum3 = parseInt(“22.5”); //returns 22
var iNum4 = parseInt(“blue”); //returns NaN
带进制参数的转換
var iNum1 = parseInt(“10”, 2); //returns 2
var iNum2 = parseInt(“10”, 8); //returns 8
var iNum2 = parseInt(“10”, 10); //returns 10
以下如果数值有前导符0,则默认为8进制转換
var iNum1 = parseInt(“010”); //returns 8
var iNum2 = parseInt(“010”, 8); //returns 8
var iNum3 = parseInt(“010”, 10); //returns 10

parseFloat()
The parseFloat() method works in a similar way to parseInt(), looking at each character starting in
position 0. It also continues until the first invalid character and then converts the string it has seen up to that point. For this method, however, the decimal point is a valid character the first time it appears. If two decimal points are present, the second is considered invalid and the parseFloat() method converts the string up until that position. This means that the string “22.34.5” will be parsed into 22.34.
在原字符串的第二个小数点处不再转换
Another difference when using parseFloat() is that the string must represent a floating-point number
in decimal form, not octal or hexadecimal. This method ignores leading zeros, so the octal number 0908 will be parsed into 908, and the hexadecimal number 0xA will return NaN because x isn’t a valid character for a floating-point number. There is also no radix mode for parseFloat()与.
与parseInt的第二点不同是:没有第二个代表进制数的参数,并且忽略前导字符0
如:
var fNum1 = parseFloat(“1234blue”); //returns 1234.0
var fNum2 = parseFloat(“0xA”); //returns NaN
var fNum3 = parseFloat(“22.5”); //returns 22.5
var fNum4 = parseFloat(“22.34.5”); //returns 22.34
var fNum5 = parseFloat(“0908”); //returns 908
var fNum6 = parseFloat(“blue”); //returns NaN

十三、Type Casting
Three type casts are available in ECMAScript
Boolean(value) – casts the given value as a Boolean
Number(value) – casts the given value as a number (either integer or floating-point)
String(value) – casts the given value a string

The Boolean() type cast returns true when the value is a string with at least one character, a number
other than 0, or an object (discussed in the next section); it returns false when the value is an empty
string, the number 0, undefined, or null.
示例如下:
var b1 = Boolean(“”); //false – empty string
var b2 = Boolean(“hi”); //true – non-empty string
var b3 = Boolean(100); //true – non-zero number
var b4 = Boolean(null); //false - null
var b5 = Boolean(0); //false - zero
var b6 = Boolean(new Object()); //true – object

The Number() type cast works in a manner similar to parseInt() and parseFloat(), except that it
converts the entire value, not just part of it.
Remember that parseInt() and parseFloat() only convert up to the first invalid character (in strings), so “4.5.6” becomes “4.5”. Using the Number() type cast, “4.5.6” becomes NaN
示例如下:
Number(false) 0
Number(true) 1
Number(undefined) NaN
Number(null) 0
Number(“5.5”) 5.5
Number(“56”) 56
Number(“5.6.7”) NaN
Number(new Object()) NaN
Number(100) 100
转換成字符串,使用String()与使用toString()的区别,不能使用null.toString()
The only difference between type casting as a string and using toString() is that the type cast can produce a string for a null or undefined value without error:
var s1 = String(null); //”null”
var oNull = null;
var s2 = oNull.toString(); //won’t work, causes an error

分享到:
评论

相关推荐

    Learn ECMAScript, 2nd Edition-Packt Publishing(2018).epub

    This book introduces the fresh and core concepts of JavaScript in the form of ECMAScript 2017 (ES8), which includes everything you'll need to get started with JavaScript and have a basic-to-advanced ...

    JavaScript基础.pdf

    ### JavaScript基础知识点详解 #### 一、脚本语言概述 **1.1 什么是脚本语言** 脚本语言(Scripting Language)是一种轻量级的编程语言,它使用ASCII字符编写,无需像C语言或Java那样经过编译过程转换为二进制...

    1-basic-types(基础类型1).pdf

    在TypeScript中,基础类型是构建复杂应用程序的基本构建块。这些类型包括布尔值、数字、字符串、数组、元组和枚举,它们都是JavaScript的核心部分,但TypeScript提供了额外的语义和工具来增强这些类型。 首先,布尔...

    计算机软件-编程源码-Javascript_VBScript教程专栏.zip

    1. **基础语法**:变量声明(var、let、const)、数据类型(基本类型与引用类型)、运算符、流程控制(条件语句、循环)等。 2. **函数**:函数定义、函数表达式、作用域、闭包。 3. **面向对象**:对象、类、原型链...

    Apply-JavaScript-Basic-Concept

    在"应用JavaScript基本概念"中,我们将深入探讨JavaScript的核心要素,包括变量、数据类型、控制结构、函数以及对象等,这些都是JavaScript编程的基础。 首先,让我们从变量开始。在JavaScript中,变量用于存储数据...

    Speaking JavaScript

    JavaScript in depth: Learn details of ECMAScript 5, from syntax, variables, functions, and object-oriented programming to regular expressions and JSON with lots of examples. Pick a topic and jump in. ...

    Basic-knowledge-of-java.zip_Knowledge

    1. **变量与数据类型**:JavaScript是动态类型的,这意味着你无需预先声明变量的数据类型。常见的数据类型包括`undefined`、`null`、`boolean`、`number`、`string`以及ES6新增的`Symbol`和`BigInt`。变量可以通过`...

    JavaScript Functional Programming for JavaScript Developers (PDF, EPUB, MOBI)

    Chapter 14: ECMAScript-2015/2016 Solutions Today Module 3: Functional Programming in JavaScript Chapter 1: The Powers of JavaScript's Functional Side – a Demonstration Chapter 2: Fundamentals of ...

    freeCodeCamp-Basic-JavaScript

    【免费编程营-Basic-JavaScript】是freeCodeCamp学习平台的一个重要部分,专注于教授初学者JavaScript的基础知识。JavaScript是一种广泛使用的编程语言,主要用于网页和网络应用的开发,赋予了网页动态交互的能力。...

    Javaweb开发 JavaScript php SQLServer2005 VisualBasic 编程技术文档1

    在IT行业中,编程技术是构建复杂系统的基础,而"Javaweb开发 JavaScript php SQLServer2005 VisualBasic 编程技术文档1"这个压缩包涵盖了几个关键的技术领域,让我们逐一深入探讨。 首先,JavaWeb开发是创建动态...

    basic-course-javascript

    JavaScript支持多种数据类型,包括基本数据类型(如字符串、数字、布尔值、null、undefined)和复杂数据类型(如对象和数组)。变量通过`var`、`let`或`const`关键字声明,它们的作用域和生命周期是理解JavaScript...

    reactjs-nodejs-basic1

    描述中提到的“basic1”,暗示了这是一个基础级别的教程,所以你可能将学习如何设置React开发环境,包括安装`create-react-app`工具,这是一个官方提供的脚手架,能够快速初始化一个新的React项目。使用`create-...

    basic-javascript:所有Javascript编码挑战

    "basic-javascript:所有Javascript编码挑战"是一系列专为初学者或编程新手设计的学习资源,旨在帮助他们掌握JavaScript的基础知识。以下是对标题和描述中提及的各个主题的详细说明: 1- 变量声明和用法: 在...

    JavaScript-Basic_Project2_141212:这是 Udacity 的 JavaScript Basic 的最终项目

    1. 变量与数据类型:JavaScript支持多种数据类型,包括基本类型(如字符串、数字、布尔值、null和undefined)和复杂类型(如对象和数组)。在项目中,学习者可能需要熟练使用变量来存储和操作这些数据。 2. 控制...

    basic-projects-js:JavaScript研究

    1. **变量和数据类型**:JavaScript支持多种数据类型,包括基本类型(如字符串、数字、布尔值、null和undefined)和复杂类型(如对象和数组)。在项目中,变量的声明和使用是基础,也是程序逻辑的基础。 2. **控制...

    javascript 和 vbscript 中文参考 帮助文件

    5. **ECMAScript**:JavaScript 的标准由 ECMA 国际制定,最新版本为 ECMAScript 2022。 JavaScript 主要应用包括: - **网页动态化**:通过操作 DOM 实现网页元素的动态变化。 - **AJAX**:异步 JavaScript 和 XML...

    VB Script JavaScript 脚本速查 ASP学习者必备

    1. **VBScript基础**:变量声明(Dim, Public, Private)、数据类型(String, Integer, Date等)、控制结构(If...Then...Else, For...Next, Do...Loop)、函数和过程的使用。 2. **JavaScript基础**:变量(var...

    webui5_jquery5-zip.zip_JavaScript/JQuery_Visual_Basic_

    随着版本的更新,ECMAScript(JavaScript 的标准)引入了更多高级特性,如箭头函数、Promise 和模块系统。 jQuery 是一个流行的 JavaScript 库,它简化了 JavaScript 的使用,提供了更简洁的 API 来操作 DOM、处理...

    脚本语言vbs,JavaScript等等

    JavaScript,另一方面,是一种基于ECMAScript规范的弱类型、解释型的、动态的、多范式的脚本语言。JavaScript最初由Netscape公司的Brendan Eich设计,主要用于客户端网页的交互,使得网页具有动态效果和用户交互性。...

Global site tag (gtag.js) - Google Analytics