Danny
Danny

Reputation: 9624

Creating javascript objects from different files

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 to create an object and call it's methods in a different file function, but it doesn't seem to work.

This is what I have so far:

person.js

function Person(name, age, gender)
{
    this.age = age;
    this.name = name;
    this.gender = gender;

    this.job;

    this.setJob = function(job)
    {
        this.job = job;
    }

    this.getAge = function()
    {
        return this.age;
    }

    this.getName = function()
    {
        return this.name;
    }

    this.getGender = function()
    {
        return this.gender;
    }
}

Job.js

function Job(title)
{
    this.title = title;
    this.description;

    this.setDescription = function(description)
    {
        this.description = description;
    }
}

main.js

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

index.html

<!DOCTYPE HTML>
<HTML>
<head>
    <title>javascript test</title>
    <script src="main.js" type="javascript"></script>
</head>
<body>
    <p id="mainBody"></p>
</body>
</HTML>

any help or advice would be greatly appreciated.

Many thanks

EDIT: Many thanks for all the answers and suggestions, however, I've included all my javascript files and still it doesn't work...

Upvotes: 10

Views: 37406

Answers (9)

Frazer Flanagan
Frazer Flanagan

Reputation: 1

I was having a similar issue and the problem for me stemmed from writing

"script src="Person.js" type="javascript"

instead of

"script src="Person.js" type="text/javascript" in my index.html file

Hope this Helps,

Upvotes: 0

Josh
Josh

Reputation: 725

ES6: https://www.sitepoint.com/understanding-es6-modules/

Supported in Safari as of Summer of 2017, but no support in other browsers. In a year or so, it seems like it'll be supported by Edge, Firefox, Chrome, Opera, and safari. So keep it in mind?

https://caniuse.com/#feat=es6-module

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94459

I see three issues with the code.

The page does not import the proper external Javascript files

<head>
    <title>javascript test</title>
    <script src="job.js" type="javascript"></script>
    <script src="person.js" type="javascript"></script>
    <script src="main.js" type="javascript"></script>
</head>

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/

Upvotes: 0

Minko Gechev
Minko Gechev

Reputation: 25682

Currently JavaScript is not clever enough to find your dependencies without help.

You need:

<!DOCTYPE HTML>
<HTML>
<head>
    <title>javascript test</title>
    <script src="person.js" type="javascript"></script>
    <script src="Job.js" type="javascript"></script>
    <script src="main.js" type="javascript"></script>
</head>
<body>
    <p id="mainBody"></p>
</body>
</HTML>

Note:

If you want on-demand load of the dependencies then you can use AMD (Asynchronous Module Definition) with requirejs or something else.

Using AMD you can define something like:

define(['Job', 'person'], function (job, person) {
    //Define the module value by returning a value.
    return function () {};
});

The define method accepts two arguments: dependencies and function which defines the module. When all dependencies are loaded they are passed as arguments to the function where is the module definition.

One more thing to notice is that Person and Job are not classes. They are just functions (constructor functions) which produces objects based on rules in their definitions.


Upvotes: 18

mattsbox
mattsbox

Reputation: 91

This isn't working because, according to your HTML code, the browser is only loading main.js

<script src="main.js" type="javascript"></script>

Since Javascript runs in the browser, not on the server where the other files are, the browser will try to execute main.js and fail, since it doesn't have access to the classes in the other files. If you include each one of the files (making sure that every file is included after the one it requires), you should have more success.

For example:

<script src="Job.js" type="javascript"></script>
<script src="person.js" type="javascript"></script>
<script src="main.js" type="javascript"></script>

Upvotes: 0

VB9-UANIC
VB9-UANIC

Reputation: 330

class methods should be defined via prototype so they receive 'this' reference, like that:

Person.prototype.getGender = function()
{
  return this.gender;
};

Upvotes: 3

twksos
twksos

Reputation: 141

you can try to include the 1st and 2nd javascript files first. like:

<!DOCTYPE HTML>
<HTML>
<head>
    <title>javascript test</title>
    <script src="person.js" type="javascript"></script>
    <script src="Job.js" type="javascript"></script>
    <script src="main.js" type="javascript"></script>
</head>
<body>
    <p id="mainBody"></p>
</body>
</HTML>

Upvotes: 2

BGerrissen
BGerrissen

Reputation: 21680

Files aren't automatically loaded, you need to add each .js file to the document with script tags and in the right order as well, otherwise you will get errors.

You might want to look into requirejs.org for dependency management, it's the hawtest thing lately untill ES6 becomes mainstream anyways.

Upvotes: 4

Mihai Stancu
Mihai Stancu

Reputation: 16107

You need to return the object created by Person in order for it to constitute a new instance of the Person prototype.

return(this);

Upvotes: 0

Related Questions