在javascript中的构造函数概念 [英] Constructor concept in javascript

查看:89
本文介绍了在javascript中的构造函数概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个问题中,我得到以下代码作为答案之一。我对语言的理解现在已经好了,
只有一个小问题。

In one of my questions, I got the following code as one of the answers. My understanding of the language has come has come a far better now, just have one small question.

var person = function() {
     this.firstName = "";
     this.lastName = "";
} 

person.prototype.showFullName = function () { 
     console.log(this.firstName + " " + this.lastName); 
} 

var perObj = new person();
perObj.firstName = "Penelope";
perObj.lastName = "Barrymore";
perObj.showFullName();

考虑对象,

var person = function() {
     this.firstName = "";
     this.lastName = "";
} 

当我使用时调用此对象时,

and when I call this object using,

var perObj = new person();

这类似于构造函数吗?

代码的时刻

var perObj = new person(); 

调用

会自动执行以下两行吗?

is invoked will the following two lines automatically get executed?

 this.firstName = "";
 this.lastName = "";

如果文件名是,还在我正在研究的其中一个博客中Samplescript.js 如果在 var Samplescript = function(){} 中使用相同的名称编写函数,是否会考虑此函数一个构造函数?
请告诉我这个。

And also in one of the blogs I was studying if the file name is Samplescript.js and if a function is written using the same name inside this like var Samplescript=function(){}, will this function be considered a constructor? Please clarify me this.

即使理论上事情很清楚,我也没有得到任何关于构造函数的令人满意的答案,在这个例子中它的编写方式很多清楚的理解。

I am not getting any satisfying answer regarding constructor practically even though theoretically things are clear, in this example the way its written makes lot of clear understanding.

推荐答案

首先,是一个常规的JavaScript函数。当你打电话时,当然是行:

First of all the person is a regular JavaScript function. When you call it, of course, lines:

this.firstName = "";
this.lastName = "";

执行

构造函数是一个概念,而不是JS语言中真正存在的东西。您需要构造函数通过调用 new MyCtr()来创建新的类似对象。同时,您需要常规函数来封装逻辑片段,并使其可以在不同的地方重复使用,而无需复制/粘贴代码。

are executed. Constructor function is rather a concept than a something really existing in the JS language. You need constructors to create new similar objects by calling new MyCtr(). At the same time you need regular functions to encapsulate piece of logic and make it reusable in different places without copy/paste the code.

您可以使用JavaScript中的所有函数构造函数。只需在函数调用表达式前添加 new 关键字。这件事改变了函数执行的 context 。如果没有 new ,则会对全局对象(浏览器中的窗口)执行该函数。并且函数内的这个变量引用了上下文。

You can use all functions in JavaScript as a constructor. Just add new keyword in front of function call expression. This thing changes the context of execution of the function. Without new the function is executed against global object (window in a browser). And this variable inside the function refers to the context.

并非每个函数都准备好构造函数。通常,构造函数正在使用这个变量做一些事情,该变量是在 new MyCtr期间创建的对象的引用( )通话。此外,构造函数永远不会返回值。

Not every function is ready to be a constructor. Usually, constructor functions are doing something with this variable which is a reference to an object which is created during new MyCtr() call. Also, constructor functions never return a value.

让我们看几个例子(你可以直接在浏览器的控制台中执行它):

Lets look at few examples (you can execute it directly in the browser's console):

function foo() {
    this.a = 1;
}

foo();  // using function as a regular function. Ctx is window.
console.log(window.a);  // prints "1"
foo.call(window);  // explicitly specify execution ctx. The same as just foo() call

var instance = new foo();  // using foo as a constructor
console.log(instance.a);   // prints "1"

// actually you can do it without new keyword
var instance = {};  // manually create new object
foo.call(instance); // manually call foo against this object
console.log(instance.a);   // prints "1"

// However, the code above is not strictly equivalent to the code using new. 
// It omits the concept of prototype, but it's enough for our current task.

关于功能和文件。像Java这样的语言中没有这样的东西,每个类必须放在单独的文件中。您可以将所有函数放在一个文件中,然后将其用作构造函数。但是,最佳做法是为每个文件(称为模块)驻留一个构造函数(读作)。

Regarding functions and files. There is no such thing in the language like in the Java that each class must be placed in the separate file. You can put all your functions within one file and then use it as constructors or not. However, the best practice is to reside one constructor function (read as class) per one file (called module).

这篇关于在javascript中的构造函数概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆