JavaScript 创建对象的新实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15505405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript creating new instance of objects
提问by user1991562
So I am designing a grade book interface and I have a course defined as:
所以我正在设计一个成绩册界面,我有一个课程定义为:
<script>
course = new Object();
var name;
var gradingareas;
var finalgrade;
</script>
then later I want to create a new instance:
然后我想创建一个新实例:
var gradingareas = new Array("Homework", "Classwork", "Exams");
course1 = new course("CS1500", gradingareas, 85);
I have also tried without the var in front to no avail. I get an "Uncaught TypeError: Object is not a function" I am very new to javascript so I don't even know if Im going about this the correct way. Any help is appreciated Thanks.
我也试过没有前面的 var 无济于事。我得到一个“未捕获的类型错误:对象不是函数”我对 javascript 很陌生,所以我什至不知道我是否以正确的方式处理这个问题。任何帮助表示赞赏谢谢。
回答by Quentin
Your existing code:
您现有的代码:
// Creates a new, empty object, as a global
course = new Object();
// Creates three new variables in the global scope.
var name;
var gradingareas;
var finalgrade;
There is no connection between the variables and the object.
变量和对象之间没有联系。
It looks like you want something more like:
看起来你想要更像:
function Course(name, gradingareas, finalgrade) {
this.name = name;
this.gradingareas = gradingareas;
this.finalgrade = finalgrade;
}
Then:
然后:
var course1 = new Course("CS1500", gradingareas, 85);
Note the use of a capital letter for naming the constructor function. This is a convention in the JS community.
请注意使用大写字母命名构造函数。这是 JS 社区的约定。
回答by Matt Styles
JS is prototypical, rather than class based and if you are new to it there are advantages to learning this immediately rather than trying to mush classical inheritance models from it, however, classical inheritance is alive and well in JS.
JS 是原型的,而不是基于类的,如果您不熟悉它,立即学习它比试图从中汲取经典继承模型更有优势,但是,经典继承在 JS 中仍然存在并且很好。
Anyhow, to answer how you would access your variables:
无论如何,要回答您将如何访问变量:
course1.name
works fine with the example above.
course1.name
与上面的例子一起工作正常。
If you wanted to privatise your data you could take this approach using closure:
如果你想私有化你的数据,你可以使用闭包来采取这种方法:
var Course = function(name, grade) {
// Private data
var private = {
name: name,
grade: grade
}
// Expose public API
return {
get: function( prop ) {
if ( private.hasOwnProperty( prop ) ) {
return private[ prop ];
}
}
}
};
Then instantiate a new object:
然后实例化一个新对象:
var course = new Course('Programming with JavaScript', 'A');
var course = new Course('Programming with JavaScript', 'A');
and start using all that private data:
并开始使用所有私人数据:
course.get('name');
course.get('name');
Of course, you'd probably want setters to manipulate that data too ;)
当然,您可能也希望 setter 操作这些数据;)
回答by Willem van der Veen
The code that you described does the following:
您描述的代码执行以下操作:
// Declares a memory variable called course and stores and object in it
var course = new Object();
// Declares three variables
var name;
var gradingareas;
var finalgrade;
These declared variables aren't automatically connected to the object. If you want these properties declared on the object you have 2 options:
这些声明的变量不会自动连接到对象。如果你想在对象上声明这些属性,你有两个选择:
- Declare them as propertiesof the object
- Declare them on the prototypeof of the object
- 将它们声明为对象的属性
- 在对象的原型上声明它们
Example1: declare them as properties of the object:
示例 1:将它们声明为对象的属性:
// Declares a memory variable called course and stores and object in it
var course = new Object();
// Access or create new properties with . or [] operator
course.name = 'math';
course.gradingareas = 'muliple';
course['finalgrade'] = 'A'
console.log(course);
Example2: Declare them on the prototype:
示例 2:在原型上声明它们:
// Create a constructor function
function Course (name, grade) {
this.name = name;
this.grade = grade;
}
// course is added on the prototype
Course.prototype.gradingareas = 'some gradingareas';
// the name and the grade are added on the object itself
var course = new Course ('willem', 10);
console.log(course);