无法理解从私有闭包中包含的 类 创建对象
Trouble understanding creating Objects from Classes contained within Private Closures
我是一名刚开始学习的学生,我一直在与 accessing/running 通过闭包中的 Class 创建的对象进行反复斗争。我掌握了 Class 创建对象的想法,但是当它们被包裹在一个私有闭包中时——我的大脑在翻译过程中丢失了一些东西。
我知道我理解这个的问题必须很简单。我只需要一点外界帮助。
我有两种情况。
场景 1:
输出:女人{年龄:20,年级:"B",class:"sophomore"}
//WORKING SCENARIO #1
var Woman = function(a, b, c){
this.age = a;
this.grade = b;
this.class = c;
}
var diana = new Woman(20, 'B','sophomore');
console.log(diana);
//END WORKING SCENARIO
情景 2:
输出:女人{年龄:未定义,年级:未定义,class:未定义}
//WHAT I'M STRUGGLING WITH
var privateOne = (function(a,b,c){
var Woman = function(a, b, c){
this.age = a;
this.grade = b;
this.class = c;
}
var person = new Woman(a,b,c);
return {
showWoman : function(d,e,f){
return person;
}
}
})();
var mary = privateOne.showWoman(20, 'B','sophomore');
console.log(mary);
//END WHAT I'M STRUGGLING WITH
听起来您需要在调用 showWoman
时实例化 Woman
:
var privateOne = (function(a,b,c){
var Woman = function(a, b, c){
this.age = a;
this.grade = b;
this.class = c;
}
return {
showWoman : function(a, b, c){
return new Woman(a, b, c);;
}
}
})();
var mary = privateOne.showWoman(20, 'B','sophomore');
console.log(mary);
我是一名刚开始学习的学生,我一直在与 accessing/running 通过闭包中的 Class 创建的对象进行反复斗争。我掌握了 Class 创建对象的想法,但是当它们被包裹在一个私有闭包中时——我的大脑在翻译过程中丢失了一些东西。
我知道我理解这个的问题必须很简单。我只需要一点外界帮助。
我有两种情况。
场景 1: 输出:女人{年龄:20,年级:"B",class:"sophomore"}
//WORKING SCENARIO #1
var Woman = function(a, b, c){
this.age = a;
this.grade = b;
this.class = c;
}
var diana = new Woman(20, 'B','sophomore');
console.log(diana);
//END WORKING SCENARIO
情景 2: 输出:女人{年龄:未定义,年级:未定义,class:未定义}
//WHAT I'M STRUGGLING WITH
var privateOne = (function(a,b,c){
var Woman = function(a, b, c){
this.age = a;
this.grade = b;
this.class = c;
}
var person = new Woman(a,b,c);
return {
showWoman : function(d,e,f){
return person;
}
}
})();
var mary = privateOne.showWoman(20, 'B','sophomore');
console.log(mary);
//END WHAT I'M STRUGGLING WITH
听起来您需要在调用 showWoman
时实例化 Woman
:
var privateOne = (function(a,b,c){
var Woman = function(a, b, c){
this.age = a;
this.grade = b;
this.class = c;
}
return {
showWoman : function(a, b, c){
return new Woman(a, b, c);;
}
}
})();
var mary = privateOne.showWoman(20, 'B','sophomore');
console.log(mary);