JavaScript笔记

  • 变量的作用域无块级作用域,只有函数作用域

  例:执行函数t()之后,会打印出什么结果呢?

var scope = "global";
function t(){
    console.log(scope);
    var scope="local";
    console.log(scope);
}
t();

其实,上面的代码相当于下面的代码:

var scope = "global";
function t(){
    var scope;
    console.log(scope);
    var scope="local";
    console.log(scope);
}
t();
console.log(scope);

由于在函数外面声明的变量scope是全局变量,所以在函数t()中,声明了一个和全局变量名字一样的局部变量,那么在这个函数里面,访问这个变量就是局部变量,全局变量被屏蔽了。结果为:

undefined
local
global

如果函数里面没有局部变量的名称与全局变量的名称一样,那么访问的就是全局变量的值。如下:

var scope = "global";
function t(){
    console.log(scope);
    var scope_local="local";
    console.log(scope_local);
}
t();

结果为:

global
local
  • 变量的创建

    在JavaScript程序中,在使用一个变量之前,必须先声明它,变量是使用关键字var声明地。如果不显式地声明一个变量,JavaScript将隐式地声明它。

    但是被隐式声明的变量总是被创建为全局变量,即使该变量只在一个函数体内使用。要防止在创建局部变量时创建全局变,就必须使用var声明。

  • 基本类型和引用类型

    数值、布尔值、null和undefined属于基本类型。对象、数组和函数属于引用类型。   

var a=[1,2,3];
var b=a;
a[0]=4;
document.write(b);

    结果为4,2,3

var a=1;
var b=a;
a[0]=4;
document.write(b);

    结果为1

  • delete功能:是删除一个对象的属性。注意,不能删除由var创建的变量。
  • 用一个函数名$()来代替非常常见但难录入的document.getElementById()来获取变量的值。
  • 类似于C语言static作用的类型:定义自己的函数属性
//create and initialize the "static" variable.
//Function declarations are processed before code is executed
uniqueInteger.counter=0;

//Here's the function. It returns a different value each time 
//it is called and uses a "static" property of itself to keep track of the //last value it returned.
function uniqueInterger(){
    //Increment and return our "static" variable
    return uniqueInteger.counter++;             
}
  •  函数的原型对象

    函数的原型对象是为了减少每个对象所需的内在数量,因为对象可以继承原型的很多属性;第二是即使是在对象创建以后才添加到原型中的属性,对象也可以继承它。

    当访问一个对象的属性时,首先是在对象的自身属性寻找,如果找不到,再到原型对象中寻找。

function Rectangle(w,h){   //看作类的构造函数
    this.width=w;
    this.height=h;  
}

//The prototype object holds methods and other properties that should be shared by each instance.
Rectangle.prototype.area=function(){return this.width*this.height;}  //原型对象

使用此类:
var r=new Rectangle(2,3);
r.hasOwnProperty("width"); //true:width is a direct property of r
r.hasOwnProperty("area");  //false:area is an inherited property of r
"area" in r; //true:"area" is a property of r
  •  实例属性、实例方法、类属性和类方法的使用
//We begin with the constructor 
function Circle(radius){
    //r is an instance property,defined and initialized in the constructor
    this.r=radius;
}

//Circle.PI is a class property--it is a property of the constructor function
Circle.PI =3.14159

//Here is an intance method that computes a circle's area
Circle.prototype.area=function(){return Circle.PI*this.r*this.r; }

//This class method takes two Circle objects and returns the one that has the larger radius.
Circle.max=function(a,b){
    if(a.r>b.r) return a;
    else return b;
}

//Here is some code that uses each of these fields:
var c=new Circle(1.0); //Create an instance of the Circle class
c.r=2.2;  //Set the r instance property
var a=c.area();  //Invoke the area() instnce method
var x=Math.exp(Circle.PI); //Use the PI class property in our own computation
var d=new Circle(1.2) ;  //Create another Circle instance 
var bigger=Circle.max(c,d); //Use the max() class method,即直接可以用,而无须创建实例
  • 编程惯例:类名首字母用大写,而对象名用小写。
  • 正则表达式直接量被定义为包含在一对斜杠(/)之间的字符,
var pattern=/s$/; //匹配所有的以字母“s”结尾的字符串

   用构造函数RegExp()也可以定义一个等价的正则表达式,代码如下

var pattern=new RegExp("s$");

    正则表达式的标志

字符 含义
i 执行不区分大小写的匹配
g 执行一个全局匹配。简而言之,即找到所有匹配,而不是在找到第一个之后就停止
m 多行模式,^匹配一行的开头和字符串的开头,$匹配一行的行尾或字符串的结尾
/Java$/im  //匹配"java"和"Java
is fun"

 程序猿必读

原文地址:https://www.cnblogs.com/longzhongren/p/4597032.html