当函数作为一个对象的属性出现时,叫做对象的方法
- var book = {
- title: null,
- dosth: function () {
- console.log(this);
- }
- }
-
- book.dosth();//输出Object
2.构造器调用模式
使用new初始化的函数调用叫做构造函数调用
- function Book(){
- this.title = 'ss';
- console.log(this);//输出Object
- console.log(this.title);//ss
- }
- Book.prototype.dosth = function(){
- console.log(this);//
- console.log(this.title);//ss
- }
- Book.dosth2 = function(){
- console.log(this);//输出function
- console.log(this.title);//undefined
- }
-
- var book = new Book();
- book.dosth();
- Book.dosth2();
3.apply调用模式
- book = {
-
- }
-
- function sayBookName(name){
- console.log(this);
- console.log(name);
- }
- sayBookName.apply(book,['hehe']);//object
4.函数调用
- function sayBookName(name){
- console.log(this);//window对象
- }