为什么 Javascript 构造函数没有 return
Why Javascript constructor doesn't return
我有问题。
当我从一个对象实例化时,它不会 return 构造函数的值。
..................................................... ..................................................... ..................................................... …………
..................................................... ..................................................... ..................................................... ..........
//Main Branch
var Creatures = function () {
function Creatures(name,alive) {
this.name = name;
this.alive = alive;
}
function Creatuers(name,alive,sleep) {
this.name = name;
this.alive = alive;
this.sleep = sleep;
}
};
var Beast = function () {
function breathe() {
return true;
}
this.__proto__ = new Creatures;
};
var Mammals = function () {
this.numberOfBeads = "i am numberOfBeads from Mammals";
this.__proto__ = new Beast;
};
//Mammals Branch
var Humans = function () {
function thinking(){
return true;
}
this.love = "i am love from humans";
this.__proto__ = new Mammals;
};
var Animals = function () {
this.instinct = "i am Animals from Animals";
this.__proto__ = new Mammals;
};
var Plants = function () {
this.color = "i am color from Plants";
function grown() {
return true;
}
this.__proto__ = new Creatures;
};
var Trees = function () {
this.height = "i am height from Trees";
this.__proto__ = new Plants;
};
var Flowers = function () {
this.smell = "i am smell from Flowers";
this.__proto__ = new Plants;
};
var Grasses = function () {
this.color = "i am color from Grasses";
this.__proto__ = new Plants;
};
var obj2 = new Creatures("ali",true);
alert(obj2.name);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<script src="sampleObject1.js"></script>
<script src="sampleObject2.js"></script>
</body>
</html>
正如@Denys Seguret 所说,我可以确认,某些事情似乎相当晦涩...
无论如何,你得到 undefined 因为函数构造函数 return 一个具有你在该函数构造函数中附加的属性的对象。
在你的例子中,你的函数是:
var Creatures = function () {
function Creatures(name,alive) {
this.name = name;
this.alive = alive;
}
function Creatures(name,alive,sleep) {
this.name = name;
this.alive = alive;
this.sleep = sleep;
}
};
这是一个函数表达式,在其作用域内有两个同名函数(第二个将覆盖第一个,尽管第一个没用)。
如您所见,您没有为将从中隐式 return 编辑的对象设置任何类型的 属性。
我让你的例子工作只是用第二个函数声明替换它:
function Creatures(name,alive,sleep) {
this.name = name;
this.alive = alive;
this.sleep = sleep;
}
var obj2 = new Creatures("ali", true);
alert(obj2.name);
这会发出一个提示 "ali"。
函数中没有任何 return 语句
var Creatures = function () {
function Creatures(name,alive) {
this.name = name;
this.alive = alive;
}
function Creatuers(name,alive,sleep) {
this.name = name;
this.alive = alive;
this.sleep = sleep;
}
};
您可能想要做的是委托对象的实例化,根据参数(可能还有一些业务逻辑)选择合适的构造函数。请注意 javascript 中没有方法重载,因此您需要为内部函数使用不同的命名。
var Creatures = function (...params) {
function Creatures1(name,alive) {
this.name = name;
this.alive = alive;
}
function Creatures2(name,alive,sleep) {
this.name = name;
this.alive = alive;
this.sleep = sleep;
}
return params.length === 2 ? new Creatures1(...params) : new Creatures2(...params);
};
var obj2 = Creatures("ali",true);
alert(obj2.name);
如果你指的是
var obj2 = new Creatures("ali",true);
它有return一个实例!它不会 return 具有属性 name
和 alive
的新 Creatures 实例,因为这些不是构造函数的参数,也不是构造函数中设置的属性 (top -一级):
var Creatures = function () {
...
};
您似乎想做的是创建重载的构造函数?!
javascript!
不支持函数重载
我有问题。 当我从一个对象实例化时,它不会 return 构造函数的值。 ..................................................... ..................................................... ..................................................... ………… ..................................................... ..................................................... ..................................................... ..........
//Main Branch
var Creatures = function () {
function Creatures(name,alive) {
this.name = name;
this.alive = alive;
}
function Creatuers(name,alive,sleep) {
this.name = name;
this.alive = alive;
this.sleep = sleep;
}
};
var Beast = function () {
function breathe() {
return true;
}
this.__proto__ = new Creatures;
};
var Mammals = function () {
this.numberOfBeads = "i am numberOfBeads from Mammals";
this.__proto__ = new Beast;
};
//Mammals Branch
var Humans = function () {
function thinking(){
return true;
}
this.love = "i am love from humans";
this.__proto__ = new Mammals;
};
var Animals = function () {
this.instinct = "i am Animals from Animals";
this.__proto__ = new Mammals;
};
var Plants = function () {
this.color = "i am color from Plants";
function grown() {
return true;
}
this.__proto__ = new Creatures;
};
var Trees = function () {
this.height = "i am height from Trees";
this.__proto__ = new Plants;
};
var Flowers = function () {
this.smell = "i am smell from Flowers";
this.__proto__ = new Plants;
};
var Grasses = function () {
this.color = "i am color from Grasses";
this.__proto__ = new Plants;
};
var obj2 = new Creatures("ali",true);
alert(obj2.name);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<script src="sampleObject1.js"></script>
<script src="sampleObject2.js"></script>
</body>
</html>
正如@Denys Seguret 所说,我可以确认,某些事情似乎相当晦涩...
无论如何,你得到 undefined 因为函数构造函数 return 一个具有你在该函数构造函数中附加的属性的对象。
在你的例子中,你的函数是:
var Creatures = function () {
function Creatures(name,alive) {
this.name = name;
this.alive = alive;
}
function Creatures(name,alive,sleep) {
this.name = name;
this.alive = alive;
this.sleep = sleep;
}
};
这是一个函数表达式,在其作用域内有两个同名函数(第二个将覆盖第一个,尽管第一个没用)。
如您所见,您没有为将从中隐式 return 编辑的对象设置任何类型的 属性。
我让你的例子工作只是用第二个函数声明替换它:
function Creatures(name,alive,sleep) {
this.name = name;
this.alive = alive;
this.sleep = sleep;
}
var obj2 = new Creatures("ali", true);
alert(obj2.name);
这会发出一个提示 "ali"。
函数中没有任何 return 语句
var Creatures = function () {
function Creatures(name,alive) {
this.name = name;
this.alive = alive;
}
function Creatuers(name,alive,sleep) {
this.name = name;
this.alive = alive;
this.sleep = sleep;
}
};
您可能想要做的是委托对象的实例化,根据参数(可能还有一些业务逻辑)选择合适的构造函数。请注意 javascript 中没有方法重载,因此您需要为内部函数使用不同的命名。
var Creatures = function (...params) {
function Creatures1(name,alive) {
this.name = name;
this.alive = alive;
}
function Creatures2(name,alive,sleep) {
this.name = name;
this.alive = alive;
this.sleep = sleep;
}
return params.length === 2 ? new Creatures1(...params) : new Creatures2(...params);
};
var obj2 = Creatures("ali",true);
alert(obj2.name);
如果你指的是
var obj2 = new Creatures("ali",true);
它有return一个实例!它不会 return 具有属性 name
和 alive
的新 Creatures 实例,因为这些不是构造函数的参数,也不是构造函数中设置的属性 (top -一级):
var Creatures = function () {
...
};
您似乎想做的是创建重载的构造函数?! javascript!
不支持函数重载