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....

















0 0
原创粉丝点击