Home > Web Front-end > JS Tutorial > Summary of JavaScript learning (1) - basics of getting started with JavaScript_javascript skills

Summary of JavaScript learning (1) - basics of getting started with JavaScript_javascript skills

WBOY
Release: 2016-05-16 15:41:08
Original
1336 people have browsed it

1. JavaScript language features

1.1. JavaScript is object- and event-driven (dynamic)

It can respond directly to user or customer input without going through the Web service program. It responds to users in an event-driven manner. The so-called event-driven refers to the action generated by performing a certain operation on the homepage, which is called an "event". For example, pressing the mouse, moving the window, selecting the menu, etc. can be regarded as events. When an event occurs, a corresponding event response may be triggered.

1.2. JavaScript is cross-platform

JavaScript is dependent on the browser itself and has nothing to do with the operating system.

2. JavaScript variables

2.1. Define variables

When defining variables, always use "var variable name", for example: var str; you can even omit the keyword var

2.2. How to determine the type of JavaScript variables

The data type of variables in JavaScript is determined by the JS engine

 var name="孤傲苍狼";//name是string类型
 var age=;//age是number类型
 var flag=true;//flag是boolean类型
 var email;//email只是声明,没有赋值,因此代表的类型是"undefined",也就是无法确定
 name=;//name自动变成了number类型
Copy after login

2.3. Use the typeof keyword to view the specific data type represented by the variable

The typeof operator has one parameter, which is the variable or value to be checked. For example:

 var sTemp = "test string";
 alert (typeof sTemp);  //输出 "string"
 alert (typeof );  //输出 "number"
Copy after login

Calling the typeof operator on a variable or value will return one of the following values:

undefined - if the variable is of type Undefined

boolean - if the variable is of type Boolean

number - if the variable is of type Number

string - if the variable is of type String

object - if the variable is a reference type or of type Null

Test code:

<script type="text/javascript">
   var name="孤傲苍狼";//name是string类型
   alert("name是"+typeof name+"类型");
   var age=;//age是number类型
   alert("age是"+typeof age+"类型");
   var flag=true;//flag是boolean类型
   alert("flag的类型是:"+typeof flag);
   name=;//name自动变成了number类型
   alert("name变量重新赋值后,name的数据类型变成了:"+typeof name);
   var email;//email只是声明,没有赋值,因此代表的类型是"undefined",也就是无法确定
   alert("email的类型是:"+typeof email );
   var a=null;
 /*
 为什么 typeof 运算符对于 null 值会返回 "Object"。
 这实际上是 JavaScript 最初实现中的一个错误,然后被 ECMAScript 沿用了。现在,null 被认为是对象的占位符,从而解释了这一矛盾,但从技术上来说,它仍然是原始值。
 */
   alert("a是"+typeof a +"类型");
  </script>
Copy after login

Run result:

3. JavaScript data types

 

JavaScript contains two different data types: basic data types and reference data types. Primitive types refer to simple data, and reference types refer to objects composed of multiple values. When we assign a value to a variable, the first thing the parser has to do is to confirm whether the value is a primitive type value or a reference type value.

3.1. Basic data types

 Five common basic data types:

Boolean
Number
String
Undifined
Null 

These five basic data types can directly operate on the actual values ​​stored in variables.

3.1.1, Numeric type (Number) and Boolean type (Boolean)

Look at the code below:

 <script type="text/javascript">
   var a = ;
   var b = a;
     b = ;
   alert("a="+a);//打印a=
   
   var b = true;
   var b = b;
     b = false;
   alert("b="+b);//打印b=true
 </script>
Copy after login

Running results:

It can be seen from the running results that the value of b is a copy of the value of a. Although the values ​​of the two variables are equal, the two variables store two different basic data type values. b just saves a copy of a's copy. So, when the value of b changes to 20, the value of a is still 10. The two Boolean variables b1 and b2 are also basic data types and also store two different basic data type values. b2 stores a copy of 1. Therefore, when the value of b2 changes to false, the value of b1 is still true.

The following figure demonstrates the process of assignment of this basic data type:

Stack memory

3.1.2. String type (String)

String in JavaScript is a special basic data type. In many languages, String is represented in the form of an object, but in JavaScript, String is treated as a basic data type and is passed by value. way to operate. But it is a rather special basic type.

Look at the example below:

<script type="text/javascript">
   var strA = "这是字符串";
   var strB = strA;
     strA = "这是另外一个字符串";
     alert("strB="+strB);
 </script>
Copy after login

运行结果:

  

  从运行结果可以看到,仿佛strA通过值传递的方式复制了一份给了strB。当strA改变的时候,strB并没有改变,似乎我们已经可以下结论,String就是个基本数据类型。

可是,因为String是可以任意长度的,通过值传递,一个一个的复制字节显示效率依然很低,看起来String也可以当作引用类型。

看下面例子:

 var a = "myobject";
 a.name = "myname";//为字符串a动态添加name属性
 alert("a.name="+a.name); //访问a的name属性,
Copy after login

结果显示:a.name=undefined

运行结果:

  

  运行结果显示,String无法当作一个对象来处理。这也证明了一点:基本类型虽然也可以添加属性,也不会报错,经测试添加完之后却是无法访问的,实际上,javascript里的String是不可以改变的,javascript也没有提供任何一个改变字符串的方法和语法。

看下面的例子:

 var b = "myobject";
 b = b.substring(,);
 alert("b="+b); // b=bj
Copy after login

运行结果:

  

  这样做,没并有改变String字符串"myobject",只b引用了另一个字符串"bj","myobject"被回收了。

  所以可以这样讲,String实际上并不符合上面两种数据类型分类。它是具有两方面属性介于两都之间的一种特殊类型。

3.1.3、Null 类型

  Null类型只有一个专用值 null,值 undefined 实际上是从值 null 派生来的,因此 ECMAScript 把它们定义为相等的。

  <script type="text/javascript">
    alert("null == undefined的结果是:"+(null == undefined)); //输出 "true"
  </script>
Copy after login

运行结果:

  

  尽管这两个值相等,但它们的含义不同。undefined 是声明了变量但未对其初始化时赋予该变量的值,null 则用于表示尚未存在的对象(typeof 运算符对于 null 值会返回 "Object"。)。如果函数或方法要返回的是对象,那么找不到该对象时,返回的通常是 null。

3.1.4、Undefined 类型

  Undefined 类型只有一个值,即 undefined。当声明的变量未初始化时,该变量的默认值是 undefined。

var oTemp;

  前面一行代码声明变量 oTemp,没有初始值。该变量将被赋予值 undefined,即 undefined 类型的字面量。可以用下面的代码段测试该变量的值是否等于 undefined:

 <script type="text/javascript">
   var oTemp;
   alert("oTemp == undefined的结果是:"+(oTemp == undefined));//输出 "true"
  </script>
Copy after login

运行结果:

  

  运行结果显示 "true",说明这两个值确实相等。

  可以用 typeof 运算符显示该变量所代表的的数据类型是undefined类型

 <script type="text/javascript">
   var oTemp;
   alert("typeof oTemp的结果是:"+typeof oTemp); //输出 "undefined"
  </script>
Copy after login

  值 undefined 并不同于未定义的值。但是,typeof 运算符并不真正区分这两种值。考虑下面的代码:

 <script type="text/javascript">
   var oTemp;
   alert("oTemp变量有声明,typeof oTemp的结果是:"+typeof oTemp); //输出 "undefined"
   alert("oTemp变量没有声明,typeof oTemp的结果是:"+typeof oTemp); //输出 "undefined"
  </script>
Copy after login

运行结果:

  两个变量输出的都是 "undefined",即使只有变量 oTemp2 从未被声明过。如果对oTemp2 使用除 typeof 之外的其他运算符的话,会引起错误,因为其他运算符只能用于已声明的变量上。

  下面的代码将引发错误:

var oTemp;
 alert(oTemp == undefined);//'oTemp' 未定义
Copy after login

  当函数无明确返回值时,返回的也是值 "undefined",如下所示:

function testFunc() { 
   //这是一个空函数,没有返回值
 }
 alert("testFunc() == undefined的结果是:"+(testFunc() == undefined)); //输出 "true"
Copy after login

运行结果:

  

3.2、引用数据类型

  javascript引用数据类型是保存在堆内存中的对象,JavaScript不允许直接访问堆内存空间中的位置和操作堆内存空间,只能通过操作对象在栈内存中的引用地址。所以引用类型的数据,在栈内存中保存的实际上是对象在堆内存中的引用地址。通过这个引用地址可以快速查找到保存在堆内存中的对象。

看下面的例子:

 <script type="text/javascript">
   var obj = new Object();
   var obj = obj;
   obj.name = "孤傲苍狼";
   alert(obj.name); // 孤傲苍狼
 </script>
Copy after login

运行结果:

  

  由上面例子,我们声明了一个引用数据类型变量obj1,并把它赋值给了另外一个引用数据类型变量obj2。当我们obj2添加了一个name属性并赋值" 孤傲苍狼"。obj1同样拥有了和obj2一样的name属性。说明这两个引用数据类型变量指向同一个对象。obj1赋值给obj2,实际只是把这个对象在栈内存的引用地址复制了一份给了obj2,但它们本质上共同指向了堆内存中的同一个对象。

下面我们来演示这个引用数据类型赋值过程

    

自然,给obj2添加name属性,实际上是给堆内存中的对象添加了name属性,obj2和obj1在栈内存中保存的只是堆内存对象的引用地址,虽然也是拷贝了一份,但指向的对象却是同一个。故而改变obj2引起了obj1的改变。

  一般而言,基本数据类型是由固定数目的字节组成,这些字节可以在解析器的较底层进行操作,比如Number和 Boolean;而引用数据类型,可以包含任意数目的属性和元素,因此它们无法像基本数据类型那样很容易的操作。

由于,引用数据类型的值是会发生变化的, 所以通过跟基本数据类型一样的值传递方式,也就没什么意义了,因为会牵涉到大量的内存的复制和比较,效率太低。所以引用数据类型是通过引用传递方式,实际传递的只是对象的一个地址。

比如Array和Function,因为它们都是特殊的对象所以它们都是引用类型。另外,引用类型是可以添加属性,基本类型虽然也可以添加属性,也不会报错,经测试添加完之后却是无法访问的。

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