简体   繁体   English

此javascript代码是什么意思?

[英]What does this javascript code mean?

function myFunc(theObject) {  
      theObject = new TheObject("Ford","Focus",2006);  
} 

Why is new TheObject() used instead of something like new Object() ? 为什么使用new TheObject()代替new Object()类的东西? I don't understand. 我不明白

有一个function TheObject(...) “类”的地方,这是建立在你的代码包含在此之前发生, 那就是它的创造。

TheObject是用户定义的对象。

For the code you posted to work, somewhere else on the same page has to be something like the following: 为了使您发布的代码正常工作,同一页上的其他位置必须类似于以下内容:

var TheObject = function(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}

Then, your posted code will create a new object with properties defined by the TheObject function. 然后,您发布的代码将创建一个具有TheObject函数定义的属性的新对象。 (In the above example, you could access the make of your new object by referencing theObject.make .) (在上面的示例中,您可以通过引用theObject.make来访问新对象的品牌。)

Here, TheObject is the type of object (class) which "theObject" is. 在此,TheObject是“ theObject”是对象(类)的类型。 The function with the same name as the type is called a constructor. 与类型同名的函数称为构造函数。 Calling it constructs a new object of that type. 调用它会构造该类型的新对象。 (eg for a TheObject type, new TheObject() creates a new object of the type TheObject) (例如,对于TheObject类型,new TheObject()创建TheObject类型的新对象)

Think of it this way: The function below makes myAuto a new Car object (of type "Car"): 可以这样考虑:下面的函数使myAuto成为新的Car对象(类型为“ Car”):

function myNewFunc(myAuto) {
  myAuto = new Car("Audi","TT",2001);
}

(It's possible the "Object" vs "TheObject" vs "theObject" terminology is confusing you. Where are you getting this sample code?) (“ Object”,“ TheObject”和“ theObject”这两个术语可能会使您感到困惑。您将从何处获得此示例代码?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM