简体   繁体   English

如何在JavaScript中调用驻留在对象方法内部的函数

[英]How do I call functions that reside inside method of an object in javascript

I have a object already written by other developer which has a method that I want to use. 我有一个由其他开发人员编写的对象,该对象具有要使用的方法。 It looks like this: 看起来像这样:

someObject : {

    sampleObject: function {

        function setAd(){
            //some code
        }
    }
}

I want use/call the function setAd() outside of object. 我想在对象外部使用/调用function setAd() Is it possible? 可能吗? I tried but it shows undefined 我尝试过,但显示undefined

No, this is not possible. 不,这是不可能的。 In javascript functions behave like variables. 在javascript函数中,行为类似于变量。 They can be for example hoisted, but more importantly they are bound to a certain scope, where they have been declared and you can call them only there. 例如,可以将它们吊起,但更重要的是,它们被绑定到已声明它们的特定范围,并且只能在此处调用它们。

You can check out this article on w3schools: https://www.w3schools.com/js/js_scope.asp 您可以在w3schools上查看此文章: https : //www.w3schools.com/js/js_scope.asp
And also: https://www.w3schools.com/js/js_function_closures.asp 并且: https : //www.w3schools.com/js/js_function_closures.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM