table of Contents
js (Javascript) - scripting language
Introduction 1. js
Javascript is an interpreted programming language that runs in the browser.
Javascript can be implemented on complex functions page, page impressions no longer a simple simple static information, but the implementation of the updated content, interactive maps, 2D / 3D animation, scrolling audio and video, etc.
Why do we have to learn Javascript?
- 1. Because you have no choice. In the web world, Javascript can only cross-platform, cross-browser web drive, interact with the user.
- ActionScript 2.Flash behind used to be popular for a while, but with the rise of mobile applications, and no one with Flash to develop mobile App, so it is now marginalized.
- 3. Conversely, as more and more popular in HTML5 and mobile PC side, JavaScript has become more important. In addition, the emerging Node.js JavaScript into the server side, JavaScript has become a versatile player.
JavaScript version:
- ECMAScript 4.1 (referred to ES4)
- The latest version of the ECMAScript 6 standard (referred to as ES6)
2. js manner of introduction
1. inline js formula
<p id="" class="" style="" onclick="console.log(2);">mjj</p>
<!--console.log() 在控制台打印 onclick='' 单击,加事件:在<p>标签上加一个单击时执行在控制台打印的时间-->
2. The embedded js
<body>
<script type="text/javascript">
//js代码
</script>
</body>
3. External js
<!--可以放在html内的任意位置-->
<script type="text/javascript" src="js/index.js"></script>
<!--在外部的js文件中写js代码-->
3. js statement
Javascript code to every word; as the end of the current statement.
1. Comment
- // a single line comment
- / ** / multi-line comment, multi-line comments shortcuts: ctrl + shift + /
2. Test Statement
console.log('hello world'); //在控制台打印内容
alert('hello world'); //弹出框显示内容
console.log(window); //打印window对象
prompt(message:'请输入今天的天气?'); //在弹出框内输入内容,prompt会有一个返回值,命名一个对象(变量)来接收,变量命名格式:使用var关键字 + 变量名
var name = prompt('请输入今天的天气?'); //接收prompt的返回值
console.log(name); //打印name对象
3. assignment operator - increment and decrement operators
<script>
var a = 1;
a ++; // 相当于a+=1;
console.log(a);
var a = 4;
//先将a的值赋值给c,再计算a++
var c = a ++;
console.log(c);//4
console.log(a);//5
//先计算a++,再将a的值赋值给c
var c = ++a;
console.log(c);//5
console.log(a);//5
</script>
4. Variable
Variables are divided into: basic data types, reference data types
4.1 Basic Data Types
Basic data types include: number (integer), string (string), Boolean (Boolean), undefined (undefined), null (empty object)
var a = 2;
var b = '2' + a; //不会报错,一个字符串+一个数字会默认将数字转成字符串再拼接
var c = true;
console.log(typeof b); //打印变量的类型
//先声明后定义——undefined
var e;
console.log(e);//查看值:undefined
console.log(typeof e);//查看类型:是undefined
var f = null;
console.log(f);
console.log(typeof f);
Note: In Print Console: Integer blue, black string
4.2 reference data types
Reference data types comprising: Array (Array), Object (Object), function (function), Date (date object)
var arr = ['张三','李四'];
console.log(arr);
//定义在对象中的函数,叫做对象的方法
var obj = {
name:'mjj',
age:19,
fav:function () {
console.log(this); //存放对象的属性
}
};
obj.fav();