Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 关于构造函数用途的混淆_Javascript - Fatal编程技术网

Javascript 关于构造函数用途的混淆

Javascript 关于构造函数用途的混淆,javascript,Javascript,我对javascript中构造函数的用途有点困惑。我知道通过使用new关键字创建构造函数也会创建全局对象的新实例。这就是一个人能做的一切吗 另外,新实例的原型与全局对象相同,那么为什么不根据需要多次使用全局对象,而不是创建实例呢?可以向新的实例添加属性吗 另外,关于构造函数,我遗漏了哪些要点?如果您能帮助理解这一点,我们将不胜感激 “我知道通过使用new关键字创建构造函数也会创建全局对象的新实例。” 那是不对的。使用new调用函数可创建一个新对象,但它不是全局对象。它是该函数的一个实例,并将该函

我对javascript中构造函数的用途有点困惑。我知道通过使用
new
关键字创建构造函数也会创建全局对象的新实例。这就是一个人能做的一切吗

另外,新实例的原型与全局对象相同,那么为什么不根据需要多次使用全局对象,而不是创建实例呢?可以向
新的
实例添加属性吗

另外,关于构造函数,我遗漏了哪些要点?如果您能帮助理解这一点,我们将不胜感激

“我知道通过使用new关键字创建构造函数也会创建全局对象的新实例。”

那是不对的。使用
new
调用函数可创建一个新对象,但它不是全局对象。它是该函数的一个实例,并将该函数作为其
.constructor
引用

原型是构造函数函数的所有实例指向的对象。它允许您的所有实例引用一组公共属性

prototype
就是构造函数创建的所有实例都包含隐式指针的对象。它允许您的所有实例引用一组公共属性

“是否可以向新实例添加属性?”

是的,它们将添加到从构造函数创建的对象中,而不是添加到其原型中

function MyConstructor( arg ) {
    // If you invoke this with "new", 
    //    then "this" will be a reference to the new instance that is returned

    this.instance_prop = arg;
}

MyConstructor.prototype.proto_prop = "baz";

var inst1 = new MyConstructor( "foo" );

var inst2 = new MyConstructor( "bar" );


console.log( inst1.instance_prop ); // foo
console.log( inst1.proto_prop );    // baz

console.log( inst2.instance_prop ); // bar
console.log( inst2.proto_prop );    // baz

编辑:下面是一个非常简单的DOM包装器示例:

构造函数在从它创建的每个实例上分别放置一个初始化为
0
count
属性

如果向构造函数传递一个字符串,它将自动调用
原型上的
fetchById
。否则,可以在返回新实例后直接调用
fetchById()

成功执行
getElementById
后,
fetchById
存储在该实例的
元素
属性上找到的元素,并调用
设置
方法,该方法存储
单击元素的
属性上的事件侦听器,并使用它向元素添加
单击
事件侦听器

侦听器只是增加该实例的计数器,并将新计数附加到元素


一旦
计数
达到
10
,就会调用
下拉
方法删除侦听器。

构造函数是Javascript启用原型继承的方式

新实例的原型不是全局对象-它是构造函数的
prototype
属性

function MyConstructor(){
    //set member variables of the instance with
    this.x = 17;
}

MyConstructor.prototype = {
    //stuff in here will be shared by all MyConstructor objects:
    foo: function(){
        //
    },
    bar = 17;
}

var x = new MyConstructor();
//x's prototype is the MyConstructor.prototype object.
//the "new" sets the prototype magically behind the scenes.
// x can now use all attributes (and methods) of its prototype as its own:
x.foo();
//this is how OO works in Javascript - there are no "classes"

//note that overwriting a variable that is defined on the prototype overwrites
// only on the instance instead;
x.bar = 42;

vay y = new MyConstructor();
y.bar; //is still 17

相关帖子:迂腐:这是该功能原型的一个实例。这并不是一个真正的例子。它只是一个对象,在它的原型链中有这个函数的原型。实际上没有实例这样的东西,因为一切都是一个对象。实例并不指向原型。他们的属性查找链中有prototype对象。@Raynos:大部分是公平点,但当你说“实例不指向原型”但又说“他们的属性查找链中有prototype对象”时,这似乎是一个奇怪的区别。当然,为了创建查找链,所创建的对象必须指向原型对象……而且,考虑到MyConstructor的
inst1实例
true
,我认为将其称为函数实例并不一定不好。它不是传统意义上的实例。@patrick_dw区别在于,说实例指向原型就是说实例是指向原型的单个指针。它不仅仅是一个指针。这种吹毛求疵的做法也没有什么价值。您不能仅仅因为有一个名为
instanceof
的操作符,就证明使用“实例”术语是合理的。那是作弊!您还忘了告诉他应该使用
对象。create
:(@Raynos:您是否同意JavaScript开发人员通常认为函数实例是指使用
new
调用该函数时呈现的对象(除了您是否认为应该使用
new
)我不是吹毛求疵,我只是想理解你的区别。
<div id="one">element number one</div>
<div id="two">element number two</div>
var wrapper1 = new DOMWrapper();  // constructor with no arguments
wrapper1.fetchById("one");        // you must fetch the element manually

var wrapper2 = new DOMWrapper( "two" ); // with a string, it will fetchById for you
function MyConstructor(){
    //set member variables of the instance with
    this.x = 17;
}

MyConstructor.prototype = {
    //stuff in here will be shared by all MyConstructor objects:
    foo: function(){
        //
    },
    bar = 17;
}

var x = new MyConstructor();
//x's prototype is the MyConstructor.prototype object.
//the "new" sets the prototype magically behind the scenes.
// x can now use all attributes (and methods) of its prototype as its own:
x.foo();
//this is how OO works in Javascript - there are no "classes"

//note that overwriting a variable that is defined on the prototype overwrites
// only on the instance instead;
x.bar = 42;

vay y = new MyConstructor();
y.bar; //is still 17