首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有多个对象的原型Object.extend,这些对象包含各自的函数

具有多个对象的原型Object.extend,这些对象包含各自的函数
EN

Stack Overflow用户
提问于 2010-03-25 11:41:43
回答 2查看 889关注 0票数 0

我怎么才能达到这样的效果呢..

代码语言:javascript
复制
var Persistence = new Lawnchair('name');

Object.extend(Lawnchair.prototype, {
  UserDefaults: {
    setup: function(callback) {
                  // "save" is a native Lawnchair function that doesnt
                  //work because
                  // "this" does not reference "Lawnchair"
                  // but if I am one level up it does. Say if I defined
                  // a function called UserDefaults_setup() it would work
                  // but UserDefaults.setup does not work.
                  this.save({key: 'key', value: 'value'});


                // What is this functions scope?
                // How do I access Lawnchairs "this"
        }
    },

    Schedule: {
        refresh: function(callback) {

        }
    }
});

//this get called but doesnt work.
Persistence.UserDefaults.setup(); 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-03-25 13:35:42

this是它自己的对象,所以“UserDefaults”指的是那里的UserDefaults。在其他语言中,结果是一个对象中的函数中的same...accessing "this“是另一个对象的属性,不会给你提供父对象。

最简单的解决方案是使用依赖注入的一个版本,并将"this“传递给较低级别的类:

代码语言:javascript
复制
var Persistence = new Lawnchair('name');

Object.extend(Lawnchair.prototype, {
  initialize: function(){
    // since initialize is the constructor when using prototype, 
    // this will always run
    this.UserDefaults.setParent(this);
  },
  UserDefaults: {
    setParent: function(parent){
        this.parent = parent;
    },
    setup: function(callback) {
                  // "save" is a native Lawnchair function that doesnt
                  //work because
                  // "this" does not reference "Lawnchair"
                  // but if I am one level up it does. Say if I defined
                  // a function called UserDefaults_setup() it would work
                  // but UserDefaults.setup does not work.
                  this.parent.save({key: 'key', value: 'value'});


                // What is this functions scope?
                // How do I access Lawnchairs "this"
        }
    },

    Schedule: {
        refresh: function(callback) {

        }
    }
});

//this get called but doesnt work.
Persistence.UserDefaults.setup(); 
票数 1
EN

Stack Overflow用户

发布于 2010-04-02 14:27:23

使用bind(this)

代码语言:javascript
复制
setup: function(callback) {
  // "save" is a native Lawnchair function that doesnt
  //work because
  // "this" does not reference "Lawnchair"
  // but if I am one level up it does. Say if I defined
  // a function called UserDefaults_setup() it would work
  // but UserDefaults.setup does not work.
  this.save({key: 'key', value: 'value'});


 // What is this functions scope?
 // How do I access Lawnchairs "this"

}.bind(this) 

与通过参数的全局变量传递相同,但以一种优雅的形式。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2512943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档