What is the `name` keyword in JavaScript?

前端 未结 4 667
温柔的废话
温柔的废话 2020-12-03 17:23

When I typed this apparently innocent snippet of code:

values.name

gedit highlighted name as a keyword. However,

相关标签:
4条回答
  • 2020-12-03 17:38

    This happened to me also, for example, the following didn't work for me while I was testing the call, bind functions.

    var name={
    firstname: 'Abhishek',
    lastname: 'Agarwal',
    
    fullname: function{
        var fname = this.firstname + ' ' + this.lastname;
        return fname;
    }
    
    function printname(){
        console.log(this.fullname());
    }
    var final=printname.bind(name);
    final();
    

    This didn't actually perform the task and instead when I replaced "name" occurences by "myname" or literally anything else, it did work!

    0 讨论(0)
  • 2020-12-03 17:40

    It's not a reserved word it's a variable (it is window.name) I'm not sure what it's defined by though.

    0 讨论(0)
  • 2020-12-03 17:45

    (I know this was asked 2 years ago but, ...) This happened to me also, for example this below would not work.

    name = document.getElementById('nombre');
    //something else
    name.className = 'thinking';
    

    Instead I changed it to

    username = document.getElementById('nombre');
    //something else
    username.className = 'thinking';
    

    and it did work! Yeah, alright that's all, but it's something I find maybe quite interesting, also because of the 'name' attribute of the 'a' tag. Something to watch out for.

    0 讨论(0)
  • 2020-12-03 17:49

    Its not a javascript reserved word, its an html attribute. Any DOM element can have a name. Looks like your syntax editor will still highlight it.

    0 讨论(0)
提交回复
热议问题