首页 > Web开发 > 详细

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

时间:2018-05-25 11:56:27      阅读:195      评论:0      收藏:0      [点我收藏+]

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高级-函数的四种调用模式

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!