object name same a function name?

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

If We have

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

function randomname(){
  alert(randomname.attribute);
}
randomname();
6条回答
  •  猫巷女王i
    2020-12-17 00:59

    forgive my pedant reply. your question is a bit confusing, or is this a tricky question. In the second line you wrote

    var randomname.attribute....
    

    if thats your point, then it is an illegal statement and will definitely throw an exception. But if your point is about the fourth line where you reused the same name.

    note that you wrote

    function randomname(){...}
    

    instead of

    var randomname  = function(){...}
    

    I dont know why, but because you used a future function, you wont be able to override preexisting names. therefore randomname will still be a hash instead of a function, thus throwing an exception.

    My question is, so what will happen to randomname function? How can we call it? Is it a wasted resource? As a general rule I always use

    var randomname = function{..}
    

    template so I am always assured that name conflicts wont happen so I never encounter this.

提交回复
热议问题