---
title: "JS笔记"
author: "kellhan"
date: "2012-09-21T17:02:16Z"
tags: []
categories: ["Web前端"]
slug: "js"
draft: false
lastmod: "2012-09-21T17:18:56Z"
id: 1683152
url: "https://kellhan.iteye.com/blog/1683152"
---

**JS灵活性中的五种方法**：

 

```
   //方法1
      function startAnimation(){
          alert("方法1：startAnimation");
      }
      startAnimation();

    //方法2
      var Anim2 = function(){
      }
      Anim2.prototype.start = function(){
          alert("方法2：start");
      }
      var test2 = new Anim2();
      test2.start();

      //方法3
      var Anim3 = function(){
      }
      Anim3.prototype = {
          start:function(){
              alert("方法3：start");
          }
      }
      var test3 = new Anim3();
      test3.start();


      //方法4
      Function.prototype.method = function(name, fn){
          this.prototype[name] = fn;
      }
      var Anim4 = function(){
      }
      Anim4.method('start',function(){
          alert("方法4：start");
      });
      var test4 = new Anim4();
      test4.start();

      //方法5
      Function.prototype.method = function(name, fn){
          this.prototype[name] = fn;
          return this;
      }
      var Anim5 = function(){
      }
      Anim5
          .method('start',function(){
              alert("方法5：start");
          })
          .method('end',function(){
              alert("方法5：end");
          });
      var test5 = new Anim5();
      test5.start();
      test5.end();
```

 

 

**js的数据类型**

基本类型：布尔类型、数值类型、字符串类型

对象类型，\[],

函数类型，

空类型，null

未定义类型，undefined

 

类型转换：

toString，把数值类型或布尔类型转换字符串类型

parseFloat/praseInt,把字符串转变成数值类型

!!,双重“非”操作把字符串或数字转变成布尔类值

 

 

**js中的函数**

1.可以存储在变量中

2.可以作为参数传给其它函数

3.可以作为返回值从其他函数中传出

4.可以在运行时进行构造

 

**匿名函数定义并立即执行**

 

```
(function(x,y){
	alert(x + y)
})(10,7);
```

 

 

 

```
var b = (function(x,y){
	return x + y;
})(10,7);
```

 

 

 

```
var b;
(function(x,y){
	b = function(){
               return  x + y;
         };
})(10,7);
b();
```

 

 

**js可以对先前定义的类和实例化的对象进行修改**

 

```
    function Person(name,age){
      this.name = name;
      this.age = age;
    }
    Person.prototype = {
      getName: function(){
        return this.name;
      },
      getAge: function(){
        return this.age;
      }
    }

    var alice = new Person('Alice',93);
    Person.prototype.getGreeting = function(){
      return 'Hi ' + this.getName() + '!';
    }

    alert(alice.getGreeting("Noer"));
```

 

 

 

 

 

 

 

 

to continue...