使用默认值定义构造函数.我这样做对吗?
Defining constructor with defaults. Am I doing this right?
我对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
变量,因为它是不必要的。
使用参数覆盖实例的默认属性没有真正的缺点。可以在创建实例后将其添加到实例中,现在在创建过程中添加它们。您的方法(由我改进)允许在创建实例时轻松创建属性。请记住,仅在该实例上设置属性。如果您希望与所有实例共享它们,请使用原型。
相关文章:
- ES6构造函数返回基类的实例
- 使用Google Visualization动态调用构造函数
- javascript中对象构造函数中的var属性与this.properties
- 理解typescript中的构造函数接口
- 如果在构造函数中有“返回”,则在 JavaScript 中的新运算符中做了什么
- 我用脚本创建了一个按钮.我可以通过点击按钮调用其他函数吗?我这样做,但失败了
- 如何模拟像 new Date() 这样的构造函数
- 使用默认值定义构造函数.我这样做对吗?
- x 不是一个函数.你期望 Object.create 用构造函数做什么
- 节点.js带方括号的全局变量和函数 - 为什么这样做
- 自定义jquery函数不允许这样做
- 如何扩展像Object或Number这样的内建函数的构造函数
- JavaScript 函数 - 为什么这样做
- 在React构造函数中调用super()做什么?
- 当像new(Date)这样编写时,如何将参数传递给构造函数
- 在JavaScript中,"return"在函数构造函数中做
- Object.defineProperty在构造函数中不做任何事情
- 对于悬停函数,这样做,如果X有类,这样做
- 一个网站我'我试图使用JavaScript取代我的setTimeout函数与点.为什么它会这样做,我该如何解决它
- 从我的构造函数对象创建一个youtube iframe视频,这样我就可以创建多个视频