开发者

What is the `name` keyword in JavaScript?

When I typed this apparently innocent snippet of code:

values.name

gedit highlighted name as a keyword. However, name is not listed by the pages linked to by an answer t开发者_JAVA技巧o a question about reserved keywords. I also did a couple trivial tests in SpiderMonkey, but name seemed to act like an ordinary identifier.

A Google search didn't tell me much either. However, I did find a page listing name in "Other JavaScript Keywords". My guess is that name is a function or a member of some DOM element and does not intrude on the namespace.

Is name really a keyword in JavaScript? If so, what does it do?


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.


(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.


What?
'name' is the property of the window object of the browser. It is a built-in property in JavaScript.
Visit: https://developer.mozilla.org/en-US/docs/Web/API/Window/name

Why @deprecated
window.name is still in use in JavaScript, but it is planned to be removed in the future.
Visit: The Difference Between Deprecated, Depreciated and Obsolete


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


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!


strangely for me too, below code did not work for me when I am learning about call, apply and bind in javascript. I was breaking my head for half an hour till I figured out I am not doing any logical mistake but in the variable name lies the problem.

var name = {firstName: 'sarwan',lastName :'kumar'};

function say(){ console.log('' + this.firstName + ' ' + this.lastName); }

say.call(name);

This gives me the result as

"undefined undefined"

And I was like literally breaking my head suspecting somehow this is not passed to say function, tried changing other parts of code except the name variable, still did not work for me. But later figured out somehow, at the expense of spoiling my mood for good half an hour.


Using the Visual Studio code editor while having enabled Eslint,you can see that 'name' is classified as '@deprecated'. Although from Google it is not clear whether 'name' is a reserved keyword or not, the '@deprecated' label hints that the feature in question however is correct maybe as an effort to support backward compatibility ... etc, currently the feature has been disapproved of and one is encouraged to avoid.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜