有没有可能保护'this'引用绑定对象内的函数

Is it possible to protect the 'this' reference binding a function within an object?

本文关键字:对象 函数 绑定 引用 有可能 this 保护      更新时间:2023-09-26

给出以下JavaScript(相关的HTML将在问题的底部发布):

var app = {
    // other objects from 'messages' array removed for brevity
    'messages': [{
        'author': 'Maya Angelou',
        'quote': "If you don't like something, change it. If you can't change it, change your attitude."
    }],
    'textProp': 'textContent' in document.body ? 'textContent' : 'innerText',
    'outputTo': document.querySelector('#output'),
    'trigger': document.querySelector('#load'),
    'quote': function () {
        var n = Math.floor(Math.random() * this.messages.length),
            f = document.createElement('figure'),
            c = document.createElement('figcaption'),
            frag = document.createDocumentFragment();
        f[this.textProp] = this.messages[n].quote;
        c[this.textProp] = this.messages[n].author;
        frag.appendChild(f);
        frag.appendChild(c);
        this.outputTo.innerHTML = '';
        this.outputTo.appendChild(frag);
    }
};

可以从对象外部调用quote()函数,使用如下命令:

document.getElementById('load').addEventListener('click', app.quote.bind(app));

JS Fiddle demo.

或者直接调用函数(不作为回调绑定到事件处理程序):

app.quote();

JS Fiddle demo.

然而,我尝试在对象本身中创建一个事件处理程序,使用:

'clickhandler': function(){
    this.trigger.addEventListener('click', this.quote);
}

JS Fiddle demo.

当然,这失败了(正如预期的那样,因为这里的this是(使用IIFE) Window对象)。

我意识到this将,而对象正在创建/在其初始化之前,引用Window对象,但是是否有一种方法我没有看到创建和触发对象本身内的事件处理?

我意识到我想象中的互联网点的很大一部分来自JavaScript,但是学习它意外地导致了完全的困惑和不足的时刻;这不是在原谅我的无知,而是在解释我的无知。

最后,HTML(如它是):

<button id="load">Switch message</button>
<div id="output"></div>

顺便说一下,我看了以下链接/建议的问题:

为了清楚起见,我试图创建对象本身,并在对象中完全创建和分配事件处理,而不必在之后调用其方法。这就是我被困住的地方(我怀疑这可能是不可能的)。

  • 是否可以在同一对象内引用对象?-我没有看到相关性(尽管也许我只是在这一点上累了)。
  • 是否有可能从子对象的函数内隐式引用父对象的成员?-这表明唯一可能的方法是通过添加一个新方法来扩展app对象的初始化,这是可行的,但确实违背了我想要做的事情(我意识到生活是不公平的,但即使如此…)。Javascript在对象中使用绑定,我如何访问对象this?-处理事件处理程序,但似乎不直接适用。不过我可能是误解了。

正如您所指定的,如果您只想创建一个新对象,您可能需要这样做。我认为无论你做什么,你仍然需要执行一些东西-无论是实例化一个对象还是运行绑定点击的特定init函数。

 var App = function App(){
    this.clickhandler()
    }
    App.prototype =
    {
        'messages': [{
            'author': 'Maya Angelou',
                'quote': "If you don't like something, change it. If you can't change it, change your attitude."
        }, {
            'author': 'Richard Feynman',
                'quote': "Hell, if I could explain it to the average person, it wouldn't have been worth the Nobel prize."
        }, {
            'author': 'Eddie Izzard',
                'quote': "Cats have a scam going – you buy the food, they eat the food, they fuck off; that's the deal."
        }, {
            'author': 'George Carlin',
                'quote': "I would never want to be a member of a group whose symbol was a man nailed to two pieces of wood. Especially if it's me!"
        }],
            'textProp': 'textContent' in document.body ? 'textContent' : 'innerText',
            'outputTo': document.querySelector('#output'),
            'trigger': document.querySelector('#load'),
            'quote': function () {
                console.log('hey')
            var n = Math.floor(Math.random() * this.messages.length),
                f = document.createElement('figure'),
                c = document.createElement('figcaption'),
                frag = document.createDocumentFragment();
            f[this.textProp] = this.messages[n].quote;
            c[this.textProp] = this.messages[n].author;
            frag.appendChild(f);
            frag.appendChild(c);
            this.outputTo.innerHTML = '';
            this.outputTo.appendChild(frag);
        },
        'clickhandler' : function(){
            this.trigger.addEventListener('click', this.quote.bind(this));
        }
    };
    //just create an object
    app = new App();
http://jsfiddle.net/LwrvT/

在某些时候,您将需要将.bind()方法替换为app(除非您避免使用this并将其替换为app)。然而,这并不一定在传递 app.quote方法的地方(例如,将绑定作为事件侦听器),但可以直接在app对象声明之后:

var app = {
    …,
    quote: function() {
        … this …
    }
};
app.quote = app.quote.bind(app);

如果周围有下划线,可以使用bindAll辅助函数:

var app = _.bindAll({
    …,
    quote: function() {
        … this …
    }
}, "quote");

如果你不在对象文本中——它可以是构造函数,IEFE,等等——你可以直接在它的声明位置.bind()函数:

function App() {
    …
    this.quote = function() {
         … this …
    }.bind(this);
}

对于coffeescript或ES6,您还可以使用fat-arrow函数语法作为糖。

除了对象文字,您还可以这样做:

var app = new function () {
    this.messages = [{
        'author': 'Maya Angelou',
            'quote': "If you don't like something, change it. If you can't change it, change your attitude."
    }, {
        'author': 'Richard Feynman',
            'quote': "Hell, if I could explain it to the average person, it wouldn't have been worth the Nobel prize."
    }, {
        'author': 'Eddie Izzard',
            'quote': "Cats have a scam going – you buy the food, they eat the food, they fuck off; that's the deal."
    }, {
        'author': 'George Carlin',
            'quote': "I would never want to be a member of a group whose symbol was a man nailed to two pieces of wood. Especially if it's me!"
    }];
    this.textProp = 'textContent' in document.body ? 'textContent' : 'innerText';
    this.outputTo =  document.querySelector('#output');
    this.trigger = document.querySelector('#load');
    this.quote = function () {
        var n = Math.floor(Math.random() * this.messages.length),
            f = document.createElement('figure'),
            c = document.createElement('figcaption'),
            frag = document.createDocumentFragment();
        f[this.textProp] = this.messages[n].quote;
        c[this.textProp] = this.messages[n].author;
        frag.appendChild(f);
        frag.appendChild(c);
        this.outputTo.innerHTML = '';
        this.outputTo.appendChild(frag);
    };
    this.trigger.addEventListener('click', this.quote.bind(this));
};

查看工作演示

这个变量只是引用app,所以就用app。

var app = {
    someVar: 'thing',
    someMethod: function(){
        alert(app.someVar);
    }
};

或者

function createApp(){
    var app = {};
    app.someVar = 'thing';
    app.someMethod = function(){
        alert(app.someVar);
    };
    return app;
}

小改动。在初始化对象的属性之前声明对象可能会对您的用例有所帮助。

var app = {};
app["messages"] = "test message";
app["textProp'] = 'textContent' in document.body ? 'textContent' : 'innerText';
app['quote']= function () {
        var n = Math.floor(Math.random() * this.messages.length),
            f = document.createElement('figure'),
            c = document.createElement('figcaption'),
            frag = document.createDocumentFragment();
        f[app.textProp] = app.messages[n].quote;
}