Execute an .js file

0

I'm doing a JS course and created an .js file in var/www/html/js/index.js . But when I try to run the file in the browser http://localhost/js/index.js it does not execute! Just show me the code I wrote in the browser.

This is my file:

function Person() {
    this.name = '';
    this.age = '';
    this.eyesColor = '';
    this.body = '';

    this.move = function() {
        //
    }

    this.say = function() {
        //      
    }

    this.see = function() {
        //      
    }

}

var lucas = new Person();

console.log(typeof Person());
console.log(typeof lucas);
    
asked by anonymous 10.10.2016 / 15:04

1 answer

2

Accessing a Javascript file (.js) directly does not cause it to run. You can create an HTML page and include the path of your script.

Ex:

<script src="index.js"></script>

You can also run directly in the browser console, F12 key in Chrome.

You can also add directly to the page, just create the <script> tag and the content inside it. Ex:

<script>
function Person() {
    this.name = '';
    this.age = '';
    this.eyesColor = '';
    this.body = '';

    this.move = function() {
        //
    }

    this.say = function() {
        //      
    }

    this.see = function() {
        //      
    }

}

var lucas = new Person();

console.log(typeof Person());
console.log(typeof lucas);
</script>
    
10.10.2016 / 15:16