码迷,mamicode.com
首页 > Web开发 > 详细

js高级-函数的四种调用模式

时间:2018-05-25 13:20:29      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:构造函数   func   习题   定义   没有   log   构造   返回   win   

1、对象方法调用模式  方法内部的this指向当前调用者的对象d

  定义类 (构造函数)

  function Dog (dogName){

    //创建一个空对象   让空对象==this

    this.name =  dogName;

    this.age = 0;

    this.run = function(){

      console.log(this.name + ‘is running...‘)

    }

    //如果函数当做构造函数来调用(new)并且没有返回任何数据的时候 默认返回this

  }

  var d= new Dog(‘wangwang‘);

  d.run();

 

2、构造器调用模式 new

  function Cat(){

    this.name = "cat"

    this.age = 19;

    this.run = function(){

      console.log(this.name + ‘is running...‘)

    }

  }

  var cat = new Cat();  //构造函数调用模式

  cat.run()  //方法调用模式

 

3、函数调用模式

  function f(a,b){

    console.log(a+‘‘+b)

    console.log(this)  window

  }

  f(2,3)

  习题

  function Dog(){

    this.age = 19;

    console.log(this)

  }

  Dog()  //window  函数调用模式

  var d = new Dog();  //d  构造函数调用模式

 

4、

js高级-函数的四种调用模式

标签:构造函数   func   习题   定义   没有   log   构造   返回   win   

原文地址:https://www.cnblogs.com/suanmei/p/9087272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!