object name same a function name?

前端 未结 6 1570
余生分开走
余生分开走 2020-12-17 00:35

If We have

var randomname = {};
randomname.attribute = \'something\';

function randomname(){
  alert(randomname.attribute);
}
randomname();
6条回答
  •  醉酒成梦
    2020-12-17 00:51

    function randomname(){
      alert(randomname.attribute);
    }
    

    is roughly the same as this:

    var randomname = function() {
      alert(randomname.attribute);
    }
    

    It's not 100% identical, since with the first syntax you can refer to functions "in the future", but this is basically how it works. There is only once namespace for functions and variables, since functions get assigned to variables.

提交回复
热议问题