Javascript 是什么让这两个功能';公共';和';私人';分别是什么意思?

Javascript 是什么让这两个功能';公共';和';私人';分别是什么意思?,javascript,oop,private,public,Javascript,Oop,Private,Public,这两个函数来自我正在学习的课程()。代码和注释来自课程提供商: /* 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.

这两个函数来自我正在学习的课程()。代码和注释来自课程提供商:

/* 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()
是“公共可访问的”,而
\u load()
是“私有的”?在这种情况下,公共/私人是什么意思


如果需要,完整文件位于

它是私有的,因为无法直接调用它。。。见第105行:

window.Resources = {
    load: load,
    get: get,
    onReady: onReady,
    isReady: isReady
};
由于该方法是在作用域中声明的,因此它在其他任何地方都不可用

您可以看到,代码写在:

(function() {
...
})()
它强制将任何函数声明或变量声明附加到当前范围。如果没有此选项,变量将附加到最近的当前对象(通常是窗口)。因此,永远不会导出
\u load
,调用它的唯一方法是调用
资源
对象中
窗口
上导出的方法之一

  • 公共是指可以从外部调用某些内容
  • 私密是指只能从内部调用某些内容

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

“Public”意味着可以从任何地方调用它。“Private”表示只能从包含此函数的文件中访问它。还要注意的是,它说“Public”函数调用“Private”函数,因此当您运行“Public”函数时,它仍然运行相同的代码。此外,请注意,许多人使用初始下划线(“”)来表示私有数据。这仅仅是一个约定,它本身实际上并不强制私有访问。但有些人把它作为区分公共和私人的唯一机制。