zoukankan      html  css  js  c++  java
  • javascript函数基础

    Function类型

    由于函数是对象,因此函数名实际上是一个指向函数对象的指针,不会与函数绑定

    所以没有重载这个概念!!!!!!!!!!!!!!!!!!!!!!!!!!

    function sum1(){}
    var sum2 = function(){}; 
     //!!!!分号!!!!将变量sum2定义成function类型

    ECMAScript中的参数

    参数在内部用arguments对象数组来访问,只关心参数数组,不关心参数个数

    function doAdd(){
        if(arguments.length == 1){
              alert(arguments[0]+10);
        }else if(arguments.length == 2){
              alert(arguments[0]+arguments[1]);
        }
    }
    
    doAdd(10);         //20
    doAdd(30,20);    //50

    arguments的值永远与对应命名参数的值保持同步

    function doAdd(num1,num2){
         arguments[1]=10;
         alert(arguments[0]+num2);
    }
    //执行这个doAdd()函数会重写第2的参数
    x+10 一直执行num1+10

    callee与caller

    caller
    caller返回一个函数的引用,这个函数调用了当前的函数。
    使用这个属性要注意:
    1 这个属性只有当函数在执行时才有用
    2 如果在javascript程序中,函数是由顶层调用的,则返回null

    functionName.caller: functionName是当前正在执行的函数。

     
     在CODE上查看代码片派生到我的代码片
    1. var a = function() {   
    2. alert(a.caller);   
    3. }   
    4. var b = function() {   
    5. a();   
    6. }   
    7. b();  


    上面的代码中,b调用了a,那么a.caller返回的是b的引用,结果如下:

     
     在CODE上查看代码片派生到我的代码片
    1. var b = function() {   
    2. a();   
    3. }   

    如果直接调用a(即a在任何函数中被调用,也就是顶层调用),返回null:

     
     在CODE上查看代码片派生到我的代码片
    1. var a = function() {   
    2. alert(a.caller);   
    3. }   
    4. var b = function() {   
    5. a();   
    6. }   
    7. //b();   
    8. a();  

    输出结果:

    null

    callee
    callee放回正在执行的函数本身的引用,它是arguments的一个属性
    使用callee时要注意:
    1 这个属性只有在函数执行时才有效
    2 它有一个length属性,可以用来获得形参的个数,因此可以用来比较形参和实参个数是否一致,即比较arguments.length是否等于arguments.callee.length
    3 它可以用来递归匿名函数。

     

     在CODE上查看代码片派生到我的代码片
    1. var a = function() {   
    2. alert(arguments.callee);   
    3. }   
    4. var b = function() {   
    5. a();   
    6. }   
    7. b();  

    a在b中被调用,但是它返回了a本身的引用,结果如下:

     
     在CODE上查看代码片派生到我的代码片
      1. var a = function() {   
      2. alert(arguments.callee);   
      3. }   

    constructor

    constructor 属性返回对创建此对象的数组函数的引用。

    object.constructor
    <script type="text/javascript">
    
    var test=new Array();
    
    if (test.constructor==Array)
    {
    document.write("This is an Array");
    }
    if (test.constructor==Boolean)
    {
    document.write("This is a Boolean");
    }
    if (test.constructor==Date)
    {
    document.write("This is a Date");
    }
    if (test.constructor==String)
    {
    document.write("This is a String");
    }
    
    </script>
    

    输出:

    This is an Array

    如何使用 constructor 属性:

    <script type="text/javascript">
    
    function employee(name,job,born)
    {
    this.name=name;
    this.job=job;
    this.born=born;
    }
    
    var bill=new employee("Bill Gates","Engineer",1985);
    
    document.write(bill.constructor);
    
    </script>
    

    输出:

    function employee(name, jobtitle, born)
    {this.name = name; this.jobtitle = job; this.born = born;}

    this

    this指的是调用函数的那个对象,this一般情况下是全局对象global,作为方法调用时,this指的是当前对象。

    apply与call

    call方法: 
    语法:call([thisObj[,arg1[, arg2[,   [,.argN]]]]]) 
    定义:调用一个对象的一个方法,以另一个对象替换当前对象。 
    说明: 
    call 方法可以用来代替另一个对象调用一个方法。call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。 
    如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。 

    apply方法: 
    语法:apply([thisObj[,argArray]]) 
    定义:应用某一对象的一个方法,用另一个对象替换当前对象。 
    说明: 
    如果 argArray 不是一个有效的数组或者不是 arguments 对象,那么将导致一个 TypeError。 
    如果没有提供 argArray 和 thisObj 任何一个参数,那么 Global 对象将被用作 thisObj, 并且无法被传递任何参数。

    prototype原型对象

    在JavaScript 中,每当定义一个对象(函数)时候,对象中都会包含一些预定义的属性。其中函数对象的一个属性就是原型对象 prototype。注:普通对象没有prototype,但有__proto__属性。

    var person = function(name){
       this.name = name
      };
      person.prototype.getName = function(){
         return this.name; 
      }
      var zjh = new person(‘zhangjiahao’);
      zjh.getName(); //zhangjiahao

    理解原型链

    JS在创建对象(不论是普通对象还是函数对象)的时候,都有一个叫做__proto__的内置属性,用于指向创建它的函数对象的原型对象prototype。以上面的例子为例:

      console.log(zjh.__proto__ === person.prototype) //true

    同样,person.prototype对象也有__proto__属性,它指向创建它的函数对象(Object)的prototype

      console.log(person.prototype.__proto__ === Object.prototype) //true

    继续,Object.prototype对象也有__proto__属性,但它比较特殊,为null

      console.log(Object.prototype.__proto__) //null

    我们把这个有__proto__串起来的直到Object.prototype.__proto__为null的链叫做原型链。如下图:

    内存结构图:


    画图约定:

     var animal = function(){};
      var dog = function(){};
    
      animal.price = 2000;//
      dog.prototype = animal;
      var tidy = new dog();
    
    
      console.log(dog.price) //undefined
      console.log(tidy.price) // 2000



    dog继承了animal,画一下内存图:

    一个完整的例子:

    function SuperType(){
         this.property = true;
    }
    
    SuperType.prototype.getSuperValue = function(){
        return this.property; 
    };
    
    function SubType(){
        this.subproperty = false;
    }
    
    subType.prototype = new SuperType(); //重新原型,继承了SuperType
    SubType.prototype.getSubValue = function(){
        return this.subproperty;
    };
    var instance = new SubType();
    alert(instance.getSuperValue());   //true
    //instance.constructor指向SuperType
    //SubType.prototype中的constructor被重写了
    //instance指向SubType的原型,SubType的原型又指向SuperType的原型

    搜索步骤:1)实例 2)SubType.prptotype 3)SuperType.prptotype

    在通过原型链实现继承时,不能使用对象字面量创建原型方法,因为这样会重写原型链

    构造函数、原型、实例的关系

    每个构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。

  • 相关阅读:
    orm 锁 和 事务
    多表查询
    django 单表查询
    djgango装饰器
    几个SQL命令的使用
    怎么成为优秀的软件模型设计者?
    jbpm 工作流(二)
    Jbpm工作流(一)
    EJB 介绍
    JNDI 使用
  • 原文地址:https://www.cnblogs.com/vanstrict/p/5679063.html
Copyright © 2011-2022 走看看