简体   繁体   中英

How to expose global variables in JavaScript

What is the best approach for exposing a global variable from an MVC Layout Master to a child view.

Here's the flow:

Layout:

var myPlugin = $("#myPlugiv").MyPlugin();

Child View:

var returnValue = myPlugin.GetValue();

This obviously works since the myPluin variable is visible to the child view, but is there another recommended pattern for sharing global values between Master Layout and the child view?

You could always organize them into a namespace to limit the number of globally scoped variables. Something like:

window.ViewModel = {
    myPlugin: $('#myPlugin').MyPlugin()
};

var returnValue = ViewModel.myPlugin.GetValue();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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