Global javascript variable inside document.ready

前端 未结 8 1875
感情败类
感情败类 2020-12-08 04:04

Which is the right way of declaring a global javascript variable? The way I\'m trying it, doesn\'t work

$(document).ready(function() {

    var intro;

    i         


        
8条回答
  •  囚心锁ツ
    2020-12-08 05:03

    If you're declaring a global variable, you might want to use a namespace of some kind. Just declare the namespace outside, then you can throw whatever you want into it. Like this...

    var MyProject = {};
    $(document).ready(function() {
        MyProject.intro = "";
    
        MyProject.intro = "something";
    });
    
    console.log(MyProject.intro); // "something"
    

提交回复
热议问题