javascript - 是什么使这两个函数分别为 'public' 和 'private',这意味着什么?

标签 javascript oop private public

这两个函数来 self 正在学习的类(class) (https://www.udacity.com/course/front-end-web-developer-nanodegree--nd001)。代码和注释来自类(class)提供者:

/* This is the publicly accessible image loading function. It accepts
 * an array of strings pointing to image files or a string for a single
 * image. It will then call our private image loading function accordingly.
 */
function load(urlOrArr) {
    if(urlOrArr instanceof Array) {
        /* If the developer passed in an array of images
         * loop through each value and call our image
         * loader on that image file
         */
        urlOrArr.forEach(function(url) {
            _load(url);
        });
    } else {
        /* The developer did not pass an array to this function,
         * assume the value is a string and call our image loader
         * directly.
         */
        _load(urlOrArr);
    }
}

/* This is our private image loader function, it is
 * called by the public image loader function.
 */
function _load(url) {
    if(resourceCache[url]) {
        /* If this URL has been previously loaded it will exist within
         * our resourceCache array. Just return that image rather
         * re-loading the image.
         */
        return resourceCache[url];
    } else {
        /* This URL has not been previously loaded and is not present
         * within our cache; we'll need to load this image.
         */
        var img = new Image();
        img.onload = function() {
            /* Once our image has properly loaded, add it to our cache
             * so that we can simply return this image if the developer
             * attempts to load this file in the future.
             */
            resourceCache[url] = img;

            /* Once the image is actually loaded and properly cached,
             * call all of the onReady() callbacks we have defined.
             */
            if(isReady()) {
                readyCallbacks.forEach(function(func) { func(); });
            }
        };

        /* Set the initial cache value to false, this will change when
         * the image's onload event handler is called. Finally, point
         * the image's src attribute to the passed in URL.
         */
        resourceCache[url] = false;
        img.src = url;
    }
}

为什么 load() 是“公开访问的”,而 _load() 是“私有(private)的”?在这种情况下,公共(public)/私有(private)意味着什么?

如果您需要,完整文件位于https://github.com/YolkFolkDizzy/frontend-nanodegree-arcade-game/blob/master/js/resources.js

最佳答案

它是私有(private)的,因为它不能被直接调用...见第 105 行:

window.Resources = {
    load: load,
    get: get,
    onReady: onReady,
    isReady: isReady
};

由于该方法是在范围内声明的,因此在其他任何地方都将不可用。

可以看到里面写了代码:

(function() {
...
})()

它强制将任何函数声明或变量声明附加到当前作用域。如果没有这个,变量将附加到最近的当前对象,通常是窗口。因此 _load 永远不会导出,调用它的唯一方法是调用 Resource 对象中的 window 导出的方法之一。

  • 公共(public)是指可以从外部调用某些东西。
  • 私有(private)是指某些东西只能从内部调用。

在 Javascript 中,私有(private)属性通常隐藏在一个作用域中,该作用域仅对在该作用域内创建的函数可用。

关于javascript - 是什么使这两个函数分别为 'public' 和 'private',这意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35415824/

相关文章:

c# - 使用存储库模式实现 Web 服务的正确方法是什么

javascript - 如何创建包含特殊函数的格式良好的全局 javascript 对象

java - OOP - 是否需要构造函数?

php - private var 在 php 类中无法按预期工作

c++ - 编译器错误 : is private within this context

javascript - 将谷歌货币转换器应用于页面上的所有价格

javascript - Spotfire - javascript 触发 IronPython 脚本在报告加载时执行

javascript - Jquery简单函数和.html()

javascript - 构造函数中的绑定(bind)与 d3 回调中的箭头函数 + React

java - Byte Buddy - 如何委托(delegate)私有(private)方法?