JavaScript function name alt ()

If the function name is "alt", I get the error: "alt is not a function". All other names work. Here is the code:

<!DOCTYPE HTML>
<html>
    <head>
    </head>
    <body>
        <input type="button" value="click me" onclick="alt('dato',28);"/>
        <script type="text/javascript">
            window.onload = function(){
                alt = function(name,age){
                    alert(name + "  " + age);
                }
            }
        </script>
    </body>
</html>

Please change the name of the function from "alt" to whatever is allowed to see that it works.

Can anyone explain what is happening?

+4
source share
3 answers

, , , onclick this . alt - , , alt HTML ( alt input, ).

, alt window.alt, script, element alt, .

, onclick="alt('dato',28);", : onclick="this.alt('dato',28);".

alt, , alt, HTML, , :

onclick="window.alt('dato',28);"
+5

, , , window.alt HTMLInputElement.prototype.alt

10 :

  • H , Scope NewObjectEnvironment (, ).

    H Window object : - .

  • null, Scope NewObjectEnvironment ( , ).

  • NULL, Scope NewObjectEnvironment (element, Scope).

: NewObjectEnvironment() ECMAScript 5 10.2.2.3 NewObjectEnvironment (O, E)

, .

  • function alt__(name,age){
      alert(name + "  " + age);
    }
    <input type="button" value="click me" onclick="alt__('dato',28)" />
    Hide result
  • window.alt (, Window ):

    <input type="button" value="click me" onclick="window.alt('dato',28)" />
    

    function alt(name,age){
      alert(name + "  " + age);
    }
    <input type="button" value="click me" onclick="window.alt('dato',28)" />
    Hide result
  • IDL content 1:

    theInput.onclick = function(){ alt('dato',28); });
    

    function alt(name,age){
      alert(name + "  " + age);
    }
    document.querySelector('input').onclick = function(){
      alt('dato',28);
    };
    <input type="button" value="click me" />
    Hide result
  • :

    theInput.addEventListener('click', function(){ alt('dato',28); });
    

    function alt(name,age){
      alert(name + "  " + age);
    }
    document.querySelector('input')
    
    
    ----------
    
    .addEventListener('click', function(){
      alt('dato',28);
    });
    <input type="button" value="click me" />
    Hide result
+3

You cannot use html attribute names as functions in attribute values ​​because it refers to the attribute value.

eg

<input type="button" value="click me" onclick="console.log(value)" />

there will be a console magazine "click me"

http://jsfiddle.net/z45dbxL4/

Implied this. Same as

<input type="button" value="click me" onclick="console.log(this.value)" />
0
source

Source: https://habr.com/ru/post/1610123/


All Articles