Creating javascript objects from different files

后端 未结 9 2856
眼角桃花
眼角桃花 2021-02-20 11:15

I\'ve been trying to do javascript for sometime now, but i want it to be \"object-orientated\" so I\'m trying to create different javascript classes in different files and try t

9条回答
  •  无人及你
    2021-02-20 11:59

    I see three issues with the code.

    The page does not import the proper external Javascript files

    
        javascript test
        
        
        
    
    

    Male needs to be a String literal

    When the interpreter encounters male within the Person constructor it looks for a variable, which it cannot find.

    function  main()
    {
        var employee = new Person("Richard", 23, "male");
        document.getElementById("mainBody").innerHTML = employee.getName();
    }
    

    The code should call the main function.

    Without a call to the main function the code is never kicked off.

    function  main()
    {
        var employee = new Person("Richard", 23, "male");
        document.getElementById("mainBody").innerHTML = employee.getName();
    }
    
    main();
    

    Working Example: http://jsfiddle.net/R7Ybn/

提交回复
热议问题