javaScript权威指南第一章 1.1 javaScript语言核心
来源:互联网 发布:windows vista32位下载 编辑:程序博客网 时间:2024/06/16 09:55
1,第一个让我学到的就是浏览器控制台的使用。
var x = "hello world";undefinedx"hello world"var book = { topic:"javaScript" , fat:true};undefinedbook.topic"javaScript"book.fattruebook.iundefinedbook.isPrototypeOffunction isPrototypeOf()var primes = [2, 3, 5, 7];undefinedprimes[primes.length-1]7primes[0]+primes[1]5function factorial(n){var p=1; for(var i = 2; i<=n; i++)p*=i;return p;}undefinedconsole.log(factorial(6));undefined720console.log(factorial(5));undefined120
2.
var x;
x = 0;
alert(x); //普通变量的使用
x = 1; //数字
x = 0.01; //整数和实数共用一种数据类型
x = "hello world"; //有双引号的文本构成的字符串
x = "javascript"; //单引号内的文本同样构成字符串
x = true; //布尔值
x = null; //null是一个特殊的值,意思是空;
x = undefined // undefined和null非常类似
//对象和数组是javaScript非常重要的数据类型;
//对象是名/值对的集合,或字符串到值映射的集合;
var book = {
topic: "JavaScript",
fat:"true"
};
//通过"." 或"[]"来访问对象属性
book.topic
book["fat"]
book.author = "Flanagan";
book.contents = {};
//javaScript数组的使用
var primes = [2, 3, 5, 7];
primes[0]
primes.length
primes[primes.length - 1]
primes[4 ] = 9; //通过赋值来添加新元素
primes[4] = 11; //通过赋值来改变已有的元素
var empty = []; //[]是空数组,它具有0个元素
empty.length //=>0
//数组和对象可以包含另一个数组或对象:
var points = [
{x:0, y:1},
{x: 1,y:1}
];
var data = {
trial1:[[1, 2],[3, 4]],
trial2:[[2, 3], [4. 5]]
}; //切记变量的最后都有";" 和function的不同
//javaScript中的表达式
3 +2 // =>5
3 -2
3 * 2
3 / 2
points[1].x - points[0].x
"3" + "2" //=>"32" 字符串连接
//算术运算符的简写形式
var count = 0;
count++;
count--;
count +=;
count *=;
count
//相等关系运算符用来判断两值是否相等,不等运算符的运算结果是true或false
var x = 2, y = 3;
x == y;
x !=y;
x <y;
x <=y;
x >y;
x >=y;
"two" == "three"
"two" > "three" //=>true 在字母表中的索引大
false == (x>y)
//函数是一段带有参数的javascript代码段,可以多次调用
function plus1(x){
return x+1;
}
plus1(y);
var square = function(x){ //函数是一种值,可以赋值给变量。
return x*x;
}; //分好标识了赋值语句的结束
square(plus1(y))
//当将函数和对象合写在一起时,函数就变成了”方法“ (method):
var a = [];
a.push(1, 2, 3);
a.reverse();
points.dist = function(){
var p1 = this[0];
var p2 = this[1];
var a = p2.x - p1.x;
var b = p2.y - p1.y;
return Math.sqrt(a * a + b * b);
};
points.dist() //1.414:
//控制语句的例子
function abs(x){
if(x >=0){
return x;
}else{
return - x;
}
}
function factorial(n){ //实现阶乘
var product = 1 ;
while (n>1){
product *= n;
n--;
}
return product;
}
function factorial(4)
function factorial2(n){ //实现阶乘
var i, product = 1;
for (i = 2; i <= n; i++)
product *= i;
return product;
}
//面向对象编程简单例子
//定义一个构造函数,初始化一个Point对象
function Point(x, y){ //构造函数均已大写字母开始
this.x = x; //关键字this指代初始化的实例
this.y = y; //将函数参数存储为对象属性
} //不需要return
//使用new关键字和构造函数创建一个实例
var p = new Point(1, 1);
//通过构造函数的prototye对象赋值
//给Point对象定义方法
Point.prototype.r = function(){
return Math.sqrt(this.x * this.x + this.y * this.y);
};
//Point的实例对象会继承方法 r()
p.r() //=>1.414....
- javaScript权威指南第一章 1.1 javaScript语言核心
- web前端之JavaScript权威指南(JavaScript语言核心)
- JavaScript权威指南第一章 概述
- JavaScript权威指南 第一章 JavaScript概述
- JS权威指南 读书笔记 第一章 JavaScript概述
- Javascript权威指南学习笔记1:第一章
- JavaScript权威指南 第一章 代码总结
- JAVASCRIPT权威指南第五版 第一章 JAVASCRIPT概述
- JavaScript基础学习之-JavaScript权威指南--第一章解释器
- 【读书笔记】JavaScript权威指南 --前言&第一章 javascript概述
- JavaScript权威指南
- JavaScript权威指南
- 《JavaScript权威指南》读书笔记
- javascript权威指南读书笔记
- JavaScript权威指南-前言
- javascript 权威指南解读
- javascript权威指南学习
- javascript 权威指南笔记
- python 正则匹配文本并返回字典列表
- 很轻微的议案U树要低啊USD
- 深入探讨 Java 类加载器
- SVN架设
- IE11也不再支持document.createElement("<input type='file' name='upload'/>");
- javaScript权威指南第一章 1.1 javaScript语言核心
- windows 访问控制模型(一)
- 每日linux命令学习之diff
- hdu 2189 悼念512汶川大地震遇难同胞——来生一起走
- openwrt开源系统LUCI配置界面
- 【索引】Probability::Examples:: Beginner
- crontab执行脚本中文乱码,手动执行没有问题
- 【思考题】任意长度有理数乘法运算
- 黑马程序员_C#基础篇总结3