Home > Web Front-end > JS Tutorial > body text

Basic tutorial for getting started with JavaScript

黄舟
Release: 2017-11-09 14:23:17
Original
1551 people have browsed it

JavaScript introductory tutorial content is all about the basic knowledge of JavaScript, allowing you to quickly understand JavaScript and become familiar with basic syntax. The content is relatively comprehensive. It is suitable for friends who have just learned JavaScript. I hope it will be helpful to you in learning JavaScript!

1. Quick Start

In the program, if you want to write js code, there are two ways:

1) In the html file, in a pair of script tags, write directly

<script language=&#39;javascript&#39;>
  document.write(&#39;hello&#39;);
</script>
Copy after login

2) In js, write directly, in html, use a pair of script tags to directly reference

<script language=&#39;javascript&#39; src=&#39;demo01.js&#39;></script>
Copy after login

The above two cannot be reused in a pair of script tags, and the file content cannot be written when quoted.

2. Basic syntax
##1. Basic format

  • JavaScript is case-sensitive

  • Variable a and variable A are two variables

  • JavaScript script program must Embed in HTML files

  • HTML tag code cannot be included in JavaScript scripts

  • <script>
      document.write(‘<table></table>&#39;);
    </script>
    Copy after login
Write one script statement per line

At the end of the statement You can add a semicolon
JavaScript script program can be saved independently as an external file

2. About the script tag
Language: Referenced language javascript, php, c#, VBSCRIPT
Src: Referenced an external js file

3. About variables
#Variables are containers used to temporarily store values. The values ​​stored in variables can be changed.
Variables must be declared before they can be used. Use var to declare variables
Use var declaration: local variable
Not use var declaration: global variable
Naming rules for variables: the first character must be an English letter, or an underscore (_); the following characters can be English letters , numbers, underscores; variable names cannot be JavaScript reserved words

Scope of variables: global variables, local variables

4, data type (if Type language, definition does not require specifying data type)
String: String '' ""
Number: Number 10, 10.01, 100
Boolean: Boolean true, false
Undefined: Undefined
Null: Empty
Object: Object type

<script language=&#39;javascript&#39;>
  //使用js描述一个人的完整信息
  var name=&#39;张三&#39;;
  var age=30;
  var marry=true;
  var height=1.8;
  document.write(&#39;<ol>&#39;);
  document.write(&#39;<li>姓名&#39;+name+&#39;</li>&#39;);
  document.write(&#39;<li>年龄&#39;+age+&#39;</li>&#39;);
  document.write(&#39;<li>婚否&#39;+marry+&#39;</li>&#39;);
  document.write(&#39;<li>身高&#39;+height+&#39;</li>&#39;);
  document.write(&#39;</ol>&#39;);
  function Person(){}
  var p1=new Person();
  p1.name=&#39;李四&#39;;
  p1.age=20;2013/12/31
  document.write(p1.name+&#39;<br>&#39;);
  document.write(p1.age+&#39;<br>&#39;);
</script>
Copy after login

5, operator
1) Arithmetic operators
+, -, *, /, %, ++, –
i++
++i

<script>
var i=10;
var j=i++;   //先赋值再自加
var k=++i;   //先自加再赋值
document.write(j);   //10
document.write(k);   //12
</script>
Copy after login

2) Comparison operators

, <, >=, <=, !=, ==, ===, !==

== and == =What's the difference?
==: Determine whether the values ​​are equal
===: Determine whether the values ​​are equal and the types are the same

<script>

var i=5;    //Number
var j="5"; //String

if(i==j){
  document.write(&#39;相等&#39;);
}
if(i===j){
  document.write(&#39;全等于&#39;);
}
</script>
Copy after login

3)

Logical operators &&, ||, !
4) Assignment operators
=, +=, -=, *=, /=, %=
Calculate the left side of the operator and the right side, and then assign the value to the left side

String operators +, += (dot is used in PHP)

3. Process structure

Sequential structure

Branch structure
Loop structure

1. Sequential structureThe code is executed line by line

2. Branch structure

If、else、else if、switch
Copy after login

3. Loop structure

For、while、do….while、for…..in
Copy after login

Small game:

Guessing game: After entering the page, a random number 1-500 will pop up, and the user will enter a number. Number, if this number is greater than a random number,

<script language=&#39;javascript&#39;>
  var n=Math.round(Math.random()*500);  // 随机数
  alert(n);
  while(true){
    var number=prompt(&#39;请输入一个0--500之间的数字&#39;); //用户输入
    if(number>n) alert(&#39;大了&#39;);
    if(number<n) alert(&#39;小了&#39;);
    if(number==n){
      alert(&#39;答对了~~~~&#39;);
      break;
    }
  }
</script>
Copy after login

4. Function
1. Function of function
Code reuse
Modular programming

2. Syntax:

Before using a function, you must first define it before you can call it

The function definition has three parts: function name, parameter list, and function body
The format of defining the function

**function 函数名([参数1,参数2…]){ 
函数执行部分; 
return 表达式; 
}**
Copy after login

Calling syntax:

Function name (actual parameter 1, actual parameter 2,...,);

3. Code example

Example 1: About the definition and calling of functions

//函数的定义
  function display(){
    alert(&#39;hello&#39;);
  }

  //函数的调用
  display();
  display();
  display();
Copy after login


Example 2: About the parameters of the

functionQuestions

在上题中,first,second是形参,i,j是实参
在函数执行过程,形参值的改变不会影响实参
按值传递

按地址传递原理图:


在js中,对象类型默认就是按地址传递


function display(obj){
  obj.name=&#39;lisi&#39;;
}

var p1=new Object();
p1.name=&#39;zhangsan&#39;;

display(p1);
alert(p1.name);//lisi
alert(p1);
Copy after login


JS的基本类型,是按值传递的。


var a = 1;
function foo(x) {
  x = 2;
}
foo(a);
console.log(a); // 仍为1, 未受x = 2赋值所影响
Copy after login


再来看对象:


var obj = {x : 1};
function foo(o) {
  o.x = 3;
}
foo(obj);
console.log(obj.x); // 3, 被修改了!
Copy after login


说明o和obj是同一个对象,o不是obj的副本。所以不是按值传递。 但这样是否说明JS的对象是按引用传递的呢?我们再看下面的例子:


var obj = {x : 1};
function foo(o) {
  o = 100;
}
foo(obj);
console.log(obj.x); // 仍然是1, obj并未被修改为100.
Copy after login


如果是按引用传递,修改形参o的值,应该影响到实参才对。但这里修改o的值并未影响obj。 因此JS中的对象并不是按引用传递。那么究竟对象的值在JS中如何传递的呢?
对于对象类型,由于对象是可变(mutable)的,修改对象本身会影响到共享这个对象的引用和引用副本。而对于基本类型,由于它们都是不可变的(immutable),按共享传递与按值传递(call by value)没有任何区别,所以说JS基本类型既符合按值传递,也符合按共享传递。

var a = 1; // 1是number类型,不可变 var b = a; b = 6;
据按共享传递的求值策略,a和b是两个不同的引用(b是a的引用副本),但引用相同的值。由于这里的基本类型数字1不可变,所以这里说按值传递、按共享传递没有任何区别。

基本类型的不可变(immutable)性质
基本类型是不可变的(immutable),只有对象是可变的(mutable). 例如数字值100, 布尔值true, false,修改这些值(例如把1变成3, 把true变成100)并没有什么意义。比较容易误解的,是JS中的string。有时我们会尝试“改变”字符串的内容,但在JS中,任何看似对string值的”修改”操作,实际都是创建新的string值。


var str = "abc";
str[0]; // "a"
str[0] = "d";
str; // 仍然是"abc";赋值是无效的。没有任何办法修改字符串的内容
Copy after login


而对象就不一样了,对象是可变的。


var obj = {x : 1};
obj.x = 100;
var o = obj;
o.x = 1;
obj.x; // 1, 被修改
o = true;
obj.x; // 1, 不会因o = true改变
Copy after login


这里定义变量obj,值是object,然后设置obj.x属性的值为100。而后定义另一个变量o,值仍然是这个object对象,此时obj和o两个变量的值指向同一个对象(共享同一个对象的引用)。所以修改对象的内容,对obj和o都有影响。但对象并非按引用传递,通过o = true修改了o的值,不会影响obj。

例3:关于函数的返回值问题


 function display(first,second){
    //函数遇到return会立即返回,后面代码不执行
    return first+second;
  }

  var i=10;
  var j=20;
  alert(display(i,j));
  document.write(display(i,j));*/
Copy after login


例4:关于匿名函数


  /*var i=function(){
    alert(&#39;hello&#39;);
  };
  i();*/
Var i=10; 变量可以保存数据,也可以保存地址

Function display(){ 
} 在window对象下添加一个叫display的变量,它指向了这个函数的首地址 
Window.i=display 我们让window对象下的i指向这个函数的首地址 
display() ======= i();
Copy after login


例5:自调用匿名函数


<script language=&#39;javascript&#39;>

  /*var i=function(){
    alert(&#39;hello&#39;);
  };
  i();*/



  (function(first){
  alert(first);
  alert(&#39;hello,js&#39;);
  })(10)

</script>
Function(){} :相当于返回首地址 
(Function(){}) :把这部分看做一个整体 
(function(){})():相当于找到这个地址并执行
Copy after login


以上这种写法:可以避免代码库中的函数有重命问题,并且以上代码只会在运行时执行一次,一般用做初始化工作。

例6:全局变量与局部变量


 <script>

  function display(){
    //var i=20; //局部变量只在局部作用域起作用
    i=20;    //全局的,会将全局i的值修改为20
  }
  display();
  alert(i);
</script>
Copy after login


在函数内部定义的就是局部的,否则就是全局的
如果函数内的变量没有var声明会直接影响全局的

为什么没有var是全局的?
是因为,在js中,如果某个变量没有var声明,会自动到上一层作用域中去找这个变量的声明语句,如果找到,就使用,如果没有找到,继续向上查找,一直查找到全局作用域为止,如果全局中仍然没有这个变量的声明语句,那么会自动在全局作用域进行声明,这个就是js中的作用域链

代码示例:


<script>

  var i=10;
  function fn1(){
    var i=100;
    function fn2(){
      i=1000;
      function fn3(){
        i=10000;
      }
      fn3();
      console.log(i);//10000
    }
    fn2();
    console.log(i);//10000
  }
  fn1();
  console.log(i);//10

</script>
Copy after login



局部访问全局使用作用域链
全局访问局部可以使用(函数)闭包进行模拟.

五、arugments的使用

在一个函数内部,可以使用arguments属性,它表示函数的的形参列表,它是以数组形式体现的

例1:在定义display函数时,它的实参个数必须要与形参个数保持一致,有时,我们定义函数时,形参数目不能固定,如何解决?


<script>

function display(){
  //没有定义形参,那么所有形参会自动存放到arguments这个属性数组中
  for(var i=0;i<arguments.length;i++){
    document.write(arguments[i]+&#39;<br>&#39;);
  }
}

display(&#39;lisi&#39;,&#39;zhangsan&#39;,&#39;wangwu&#39;);  //三个实参
display(&#39;zhangsan&#39;,&#39;lisi&#39;,&#39;wangwu&#39;,&#39;xiaoqiang&#39;,&#39;wangcai&#39;); //五个实参


</script>
Copy after login


如果定义时,参数个数不确定,可以通过arguments来保存所有实参

例2:使用js函数来计算每个公司的员工工资总额


<script>

  function display(){
    var sum=0; //总额
    for(var i=0;i<arguments.length;i++){
      sum+=arguments[i];
    }
    document.write(sum+&#39;<br>&#39;);
  }

  //A公司
  display(10000,2000,5000);
  //B公司
  display(1000,2000,5000,8000,10000);
</script>
Copy after login


相关文章推荐:

1.JavaScript入门教程:2017年最好的7个JS入门教程推荐

2.JavaScript入门学习的经验分享

3.javascript入门学习指南新手篇

The above is the detailed content of Basic tutorial for getting started with JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!