Pro JavaScript Techniques 学习笔记1

JavaScript 根本是一种动态语言 在运行时解析
           而且应该是顺序解析运行的 即定义过的才可以使用

. JavaScript 大小写敏感, 但是对字符串 单引号和双引号 不敏感


.Object-Oriented  特性

JavaScript 使用 function 仿照对象
即 function 有 两种含义
一种是 本来的 函数 调用的意义
另一种是 OO 中 class 的意义

同时 JavaScript 中任何function 也是一个对象 Object

   function Lecture( name, teacher ) {
     // Save them as local properties of the object
     this.name = name;
     this.teacher = teacher;
   }
   可以
     var obj=Lecture;
   然后调用
     new Obj("bob","John");
 
   也可以
     var obj=new Lecture("bob","John");
   但此时不可以  
     new Obj("bob","John");
     
  如 Lecture 有返回值
  调用
     var obj=Lecture(); 一样合理
  但要区分
     var obj=Lecture ;  obj 是 function 对象
     var obj=Lecture(); obj 是 function 调用的返回值
                            返回值同样可以是function  但此function必须是已定义过的
                            换句话说  就是function本身 和 后面定义function 的都不可以
     var obj=new  Lecture(); obj 是 一个对象实例  有它自己的属性
     
可以把
   function 理解为 class
   new function() (比如 new  Lecture()) 理解为产生一个对象的实例 ,它有自己的属性

同时注意 JavaScript 中的function 的调用不做具体的 type——validate
即不验证它的参数类型和参数数量 也不验证它的返回值类型
因此 没有重载概念
后面定义的function 将覆盖前面的定义
从而引出命名空间(namespace的概念)

同时需要注意的是this 的context 含义
   如直接调用  Lecture()                    this 指代的是 缺省的 window 对象
   如调用  new Lecture("bob","John")          this 指代 新的 匿名对象
   如调用  var obj=new Lecture("bob","John")  this 指代 obj


使用匿名函数
  (function(){
   // Save them as local properties of the object
     alert("aaa");
   })();




function 的缺省参数 arguments
在function调用是 系统会自动把 arguments 传入函数
arguments 是一个 array 对象 但是不能在函数总改变

function的另两种调用方法
   function.call(obj,arg1,arg2);
   function.apply(obj,arguments);


Public ,Private methods

public methods 通过
prototype (object) 添加

User.prototype.getName = function(){
return this.name;
};

private methods 一般在 contructor内

// An Object constructor that represents a classroom
function Classroom( students, teacher ) {
// A private method used for displaying all the students in the class
function disp() {
alert( this.names.join(", ") );
}
// Store the class data as public object properties
this.students = students;
this.teacher = teacher;
// Call the private method to display the error
disp();
}
// Create a new classroom object
var class = new Classroom( [ "John", "Bob" ], "Mr. Smith" );
// Fails, as disp is not a public property of the object
class.disp();

Privileged Methods 方法能调用 属性不能访问
// Create a new User object constructor
function User( name, age ) {
// Attempt to figure out the year that the user was born
var year = (new Date()).getFullYear() – age;
// Create a new Privileged method that has access to
// the year variable, but is still publically available
this.getYearBorn = function(){
return year;
};
}

变量的属性可以这样赋值,从而实现动态变化
Obj["属性名"]=类型 (任何类型,包括function)
其中的 obj 也可以是 function对象
不过函数对象的属性 不会 通过 new 后自动给 对象实例
有点像 java class 的 static



5.JavaScript 中的类型
        Type-Checking Variables   Variable typeof Variable    Variable.constructor
          { an: “object” }                object                    Object
         [ “an”, “array” ]                object                    Array
            function(){}                 function                   Function
           “a string”                      string                   String
              55                          number                    Number
            true                          boolean                   Boolean
           new User()                     object                     User
 
 
 
 
  可以用
    var obj={ pro1: "object" , pro2:111,func:function(){} }
    来直接实例化一个对象

  其效果相当于
    function funcObj(pro1,pro2){
      this.pro1=pro1;
      this.pro2=pro2;
    };
    funcObj.prototype.func=function(){};

    var obj=new funcObj("object",111);
    
   var obj = new Object();
// Set some properties of the object to different values
   obj.pro1 = 5;
   obj.pro2 = 5;
   obj.func = function(){
   };


 所有变量都有     缺省属性 constructor
     function 还有缺省属性 prototype
 
 还可以用 typeof 来进行类型判断
// Check to see if our number is actually a string
if ( typeof num == "string" )
// If it is, then parse a number out of it
num = parseInt( num );

或者使用constructor来进行类型判断
// Check to see if our number is actually a string
if ( num.constructor == String )
// If it is, then parse a number out of it
num = parseInt( num );
constructor 可以这么用
 var you = new me.constructor();


5 所有变量是 references (引用)
  是对象的引用 不是 引用的引用
  即任何变量是直接对应到实际对象的
 
 是  A--object --b
          |
          |
          c
不是  a--b--c--ojbect
       
例如:       
 // Set obj to an empty object
var obj = new Object();
// objRef now refers to the other object
var objRef = obj;
// Modify a property in the original object
obj.oneProperty = true;
// We now see that that change is represented in both variables
// (Since they both refer to the same object)
alert( obj.oneProperty === objRef.oneProperty );


但是 对于 string ,当 变量变化时  会自动产生一个新的string 对象
例如
// Set item equal to a new string object
var item = "test";
// itemRef now refers to the same string object
var itemRef = item;
// Concatenate some new text onto the string object
// NOTE: This creates a new object, and does not modify
// the original object.
item += "ing";
// The values of item and itemRef are NOT equal, as a whole
// new string object has been created
alert( item != itemRef );


7.变量的范围

In JavaScript, scope is kept within functions, but not within blocks (such as while, if, and for statements).
即JavaScript的变量是最小有效范围是 function

An interesting aspect of browser-based JavaScript is that all globally scoped variables are actually just properties
of the window object.
同时 所以的全局变量都是window的属性

注意下面的例子
function test() {
   foo = "test";
  }
test();
alert( window.foo == "test" ); true
 
function test() {
  var foo = "test";
  }
 test();
 alert( window.foo == "test" ); false  
 
 忘记 var 后 将 把foo 作为 全局变量
 
 this 上下文的问题
 也是根据顺序动态解析的
  var obj =  {handleMsg: function( msg ) {
      this.val=msg;
      alert( "This is a custom message: " + this.val );
   } ,handleMsg1: function( msg ) {
     alert( "This is a custom message: " + msg );
   } };
   window.handleMsg=obj.handleMsg;
   window.handleMsg("bbb"); //在这个function 调用后 window 将拥有 val 变量
   alert(window.val);
 
 
 
 8 Closures 闭包
   http://jibbering.com/faq/faq_notes/closures.html.
   例如:
   function test() {
     // Save them as local properties of the object
     alert("aaa");
   };
   setTimeout("test()", 3000);
   setTimeout(test, 3000);
   能正确调用
   其关键是对 function 的使用 ,和java 的 reflect 发射机制是一个道理???
  
最后  Listing 2-25. 例子有误  晕
btw : google 确实比 baidu 强
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值