本文介绍了从不同的文件创建javascript对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我一直试图做某些时候的javascript,但我想要的是面向对象,所以我试图在不同的文件中创建不同的javascript类,并尝试创建一个对象,并调用它的方法一个不同的文件功能,但它似乎不工作。 这是我到目前为止: person.js function Person(name,age,gender) { this.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测试< / title> < script src =main.jstype =javascript>< / script> < / head> < body> < p id =mainBody>< / p> < / body> < / HTML> 任何帮助或建议都将非常感激。 非常感谢 编辑:非常感谢所有的答案和建议,但是,我包括所有的javascript文件, t工作... 解决方案目前JavaScript不够聪明,无法帮助您找到您的依赖。 您需要: <!DOCTYPE HTML& < HTML> < head> < title> javascript测试< / title> < script src =person.jstype =javascript>< / script> < script src =Job.jstype =javascript>< / script> < script src =main.jstype =javascript>< / script> < / head> < body> < p id =mainBody>< / p> < / body> < / HTML> 注意: / p> 如果您希望按需加载依赖关系,则可以使用 AMD (异步模块定义)与 requirejs 或其他。 使用AMD你可以定义如下: define(['Job','person'],function(job,person){ //通过返回值定义模块值。 return function(){}; }); define方法接受两个参数:dependencies和function定义模块。 当所有依赖关系都被加载时,它们作为参数传递给函数,其中是模块定义。 还要注意的是 Person 和 Job 不是类。它们只是根据定义中的规则生成对象的函数(构造函数)。 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.jsfunction 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.jsfunction Job(title){ this.title = title; this.description; this.setDescription = function(description) { this.description = description; }}main.jsfunction 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 thanksEDIT:Many thanks for all the answers and suggestions, however, I've included all my javascript files and still it doesn't work... 解决方案 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. 这篇关于从不同的文件创建javascript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-24 00:10