【发布时间】:2015-11-05 10:44:37
【问题描述】:
如果问题只是愚蠢且无法回答,请告诉我?
我刚刚开始使用 javascript 进行单元测试。我一直在我的应用程序中实现this 博客代码。我只是对在 javascript 中传递给函数的名称参数感到困惑。如果我们谈论 Java、Php 或任何其他语言,它们在构造函数中接受参数,并且可以在类中使用类似
$vehicle = new Vehicle('argument here');
Class Vehicle {
protected $name;
function __construct($name) {
$this->name = $name; // we can use the property any where in the class
}
}
//Is name Property? Object? or anything else?
//javascript
(function(name) {
"use strict";
function vehicle(modal) {
this.modal = modal || 'Civic 2015';
}
name.vehicle = vehicle; //? ? ?
})(this);//also why using (this) ?
也非常欢迎举个例子。
【问题讨论】:
-
请阅读关于 JavaScript 中 OOP 的优秀教程。 MDN 有一个可用的
标签: javascript function oop