使用默认值定义构造函数.我这样做对吗?

Defining constructor with defaults. Am I doing this right?

本文关键字:这样做 构造函数 默认值 定义      更新时间:2023-09-26

我对javascript很陌生,试图理解基础知识。来自Python,我对完成工作的不同方法有点不知所措。

我想出了下面的模式来制作构造函数。我希望这是合理的做法:是吗?一些具体问题仍然存在:

  • 我立即跳到做_this = this,只使用_this.这有什么坏处吗?
  • 我允许构造函数将对象作为第一个参数。对象属性变为"命名参数"并覆盖默认值。这有什么缺点吗?

抱歉,我的问题不是很具体,但在这个阶段我需要一些确认/批评。

var Omelette = function () {
    var _this = this;
    _this.publicName = 'Dexter';
    _this.publicEggs = 6;
    // Override property values with custom supplied values
    // This comes after the default values and before they are used
    for (var p in arguments[0]) {
        _this[p] = arguments[0][p];
    }
    var privateGreeting = 'Hello, ';
    _this.publicSayHi = function () {
        console.log(privateGreeting + _this.publicName );
    };
    var privateBreakEggs = function (brokenEggs) {
        _this.publicEggs = _this.publicEggs - brokenEggs;
    };
    // initializing the prototype breaks two eggs
    privateBreakEggs(2);
    console.log(_this.publicEggs);
};
var myOmelette = new Omelette({
     publicName: 'Roy'
});
// 4
var hisOmelette = new Omelette({
     publicEggs: 12
});
// 10
hisOmelette.publicSayHi();
// Hello Dexter
hisOmelette.publicEggs = 12;
// No more eggs are broken
console.log(myOmelette.publicEggs);
// These are unchanged
hisOmelette.breakEggs();
// not publicly accessible. error 'undefined is not a function'  

请看下面的代码:

	var Omelette = function (overrideObject) {
		this.publicName = 'Dexter';
		this.publicEggs = 6;
		// Override property values with custom supplied values
		if (overrideObject)
		{
			var keys = Object.keys(overrideObject); //get all the keys from the override object.
                        //We use Object.keys for this. -> puts all the keys of an object into an array.
			for (var i = 0; i < keys.length; ++i)
			{
				this[keys[i]] = overrideObject[keys[i]]; //loop over every key and overwrite/set the value a public variable.
			}
		}
		var privateGreeting = 'Hello, ';
		this.publicSayHi = function () {
			console.log(privateGreeting + this.publicName ); //this refers to the instance created and the owner of function publicSayHi.
		};
		var privateBreakEggs = function (brokenEggs) {
			this.publicEggs = this.publicEggs - brokenEggs;
		};
		
		this.breakEggs = privateBreakEggs; //reference to privateBreakEggs, making it publically available.
	
		//this won't work since the this in privateBreakEggs refers to its own scope.
		privateBreakEggs(2);
		
		//this will work since it's called via `this`.
		this.breakEggs(2);
	};
	var myOmelette = new Omelette({
		 publicName: 'Roy'
	});
	myOmelette.publicSayHi(); //should say Roy.
	var hisOmelette = new Omelette({
		 publicEggs: 12
	});
	hisOmelette.publicSayHi(); //should say Dexter
	console.log(hisOmelette.publicEggs); //10
	console.log(myOmelette.publicEggs); //4
	// These are unchanged
	hisOmelette.breakEggs(2); //eggs now 8
	console.log(hisOmelette.publicEggs)
	
	hisOmelette.privateBreakEggs
	// not publicly accessible. 'undefined'

我对您的代码进行了一些更改并进行了评论。将其与您自己的代码进行比较,并从差异中学习。我不再引入_this变量,因为它是不必要的。

使用参数覆盖实例的默认属性没有真正的缺点。可以在创建实例后将其添加到实例中,现在在创建过程中添加它们。您的方法(由我改进)允许在创建实例时轻松创建属性。请记住,仅在该实例上设置属性。如果您希望与所有实例共享它们,请使用原型。