gpt4 book ai didi

javascript - modal.js 中的匿名函数定义

转载 作者:行者123 更新时间:2023-11-30 16:59:31 25 4
gpt4 key购买 nike

大家好,我刚刚浏览了 modal.js 的资源,对 Javascript 以及 modal.js 插件中使用的编码约定有一些疑问,

Modal.js plugin .

好吧,如果你看到line 76 ,注意ananimous函数是如何开始的:

this.backdrop(function () {

我是 javascript 的新手,我很少看到类似的东西,为什么不做以下事情:

(function () {

this.backdrop 在哪里定义我看到 this.$backdrop 定义但没有this.backdrop

其次,我有一个应该相对简单的问题,但我只是想确认以确保,即使我进行了一些测试,问题是在使用原型(prototype)时这指向什么。看看下面的框架代码:

+function ($) {
'use strict';

// MODAL CLASS DEFINITION
// ======================

var Modal = function (element, options) {

this.options w = options
this.$body = $(document.body)
this.$element = $(element)
this.$backdrop =
this.isShown = null
this.scrollbarWidth = 0

if (this.options.remote) {
this.$element
.find('.modal-content')
.load(this.options.remote, $.proxy(function () {
this.$element.trigger('loaded.bs.modal')
}, this))
}
}

Modal.VERSION = '3.3.2'

Modal.TRANSITION_DURATION = 300
Modal.BACKDROP_TRANSITION_DURATION = 150

Modal.DEFAULTS = {
backdrop: true,
keyboard: true,
show: true
}

Modal.prototype.checkScrollbar = function () {

}

Modal.prototype.setScrollbar = function () {

}

Modal.prototype.resetScrollbar = function () {

}

Modal.prototype.measureScrollbar = function () {

}

}(jQuery);

例如,这在里面指的是什么。 Modal.prototype.measureScrollbar ??模态??或 Modal.prototype.setScrollbar 如果我 console.log 这个我得到 Object { options: Object, $body: Object, $element: Object, isShown: false, $backdrop: null, scrollbarWidth: 17, bodyIsOverflowing: false } 但是为什么难道我没有得到 Modal.prototype.setScrollbar , Modal.prototype.resetScrollbar ,Modal.prototype.measureScrollbar 即使它们是模态的一部分。

请注意,我的主要困难在于 this.backdrop(function() { })

的编码约定

如果有任何帮助,我将不胜感激。

谢谢。

亚历山大。

最佳答案

Why...this.backdrop(function () {

因为 backdrop 函数需要回调(见下文)。

where is the this.backdrop defined i see this.$backdrop defined but not this.backdrop ?

它作为原型(prototype)的一部分在第 186 行定义:

Modal.prototype.backdrop = function

这意味着当模式被实例化时(例如 new Modal),创建的对象将有一个 backdrop 功能。这就是 JavaScript 原型(prototype)继承的工作原理。也许查看这篇文章以获得介绍:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain

关于javascript - modal.js 中的匿名函数定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29133587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com