JavaScript对象和getter属性
我想不出来。我怎么会得到JavaScript对象和getter属性,javascript,jquery,Javascript,Jquery,我想不出来。我怎么会得到volume1和volume2属性的NaN和null undefined=''[0]; 函数集(默认值){ $。扩展(此{ repCount:未定义, 重量:未定义, 获取卷1(){ 返回this.repCount*this.weight; } }); 如果(默认值){ $.extend(这是默认值); } } $.extend(Set.prototype{ 获取卷2(){ 返回this.repCount*this.weight; } }); var firstSet=
volume1
和volume2
属性的NaN
和null
undefined=''[0];
函数集(默认值){
$。扩展(此{
repCount:未定义,
重量:未定义,
获取卷1(){
返回this.repCount*this.weight;
}
});
如果(默认值){
$.extend(这是默认值);
}
}
$.extend(Set.prototype{
获取卷2(){
返回this.repCount*this.weight;
}
});
var firstSet=新集合({
体重:135,
重复次数:8次
});
document.write(JSON.stringify(firstSet));
文件。写(“
”);
文件编写(第一套,第2卷)代码>
jQuery的extend
不复制getter/setter-
如果没有get
it,它可以工作:
function Set(defaults) {
$.extend(this, {
repCount: undefined,
weight: undefined,
volume1: function() {
return this.repCount * this.weight;
}
});
if (defaults) {
$.extend(this, defaults);
}
}
$.extend(Set.prototype, {
volume2: function() {
return this.repCount * this.weight;
}
});
var firstSet = new Set({
weight: 135,
repCount: 8
});
document.write(JSON.stringify(firstSet));
document.write("<br />");
document.write(firstSet.volume2());
功能集(默认值){
$。扩展(此{
repCount:未定义,
重量:未定义,
第1卷:函数(){
返回this.repCount*this.weight;
}
});
如果(默认值){
$.extend(这是默认值);
}
}
$.extend(Set.prototype{
第2卷:函数(){
返回this.repCount*this.weight;
}
});
var firstSet=新集合({
体重:135,
重复次数:8次
});
document.write(JSON.stringify(firstSet));
文件。写(“
”);
document.write(firstSet.volume2());
jQuery的extend
不复制getter/setter-
如果没有get
it,它可以工作:
function Set(defaults) {
$.extend(this, {
repCount: undefined,
weight: undefined,
volume1: function() {
return this.repCount * this.weight;
}
});
if (defaults) {
$.extend(this, defaults);
}
}
$.extend(Set.prototype, {
volume2: function() {
return this.repCount * this.weight;
}
});
var firstSet = new Set({
weight: 135,
repCount: 8
});
document.write(JSON.stringify(firstSet));
document.write("<br />");
document.write(firstSet.volume2());
功能集(默认值){
$。扩展(此{
repCount:未定义,
重量:未定义,
第1卷:函数(){
返回this.repCount*this.weight;
}
});
如果(默认值){
$.extend(这是默认值);
}
}
$.extend(Set.prototype{
第2卷:函数(){
返回this.repCount*this.weight;
}
});
var firstSet=新集合({
体重:135,
重复次数:8次
});
document.write(JSON.stringify(firstSet));
文件。写(“
”);
document.write(firstSet.volume2());
进行了一些调试,我看到您的getter方法使用“undefined”实例在构造函数中将这两个实例相乘。所以undefined*undefined将返回NaN。我改变了脚本构造Set对象的方式以使其工作,它现在返回一个数字作为构造函数中对象集的属性,并且没有默认值undefined
通过设置document.write以获取卷1,更改getter以返回this.repCount,并将repCount设置为0或未定义,您可以看到它正在执行我所描述的操作:
undefined = ''[0];
function Set(defaults) {
$.extend(this, {
repCount: 0,
weight: 0,
get volume1() {
return this.repCount;
}
});
if (defaults) {
$.extend(this, defaults);
}
}
$.extend(Set.prototype, {
get volume2() {
return this.repCount;
}
});
var firstSet = new Set({
weight: 135,
repCount: 8
});
document.write(JSON.stringify(firstSet));
document.write("<br />");
document.write(firstSet.volume2);
undefined=''[0];
函数集(默认值){
$。扩展(此{
重复次数:0,
重量:0,,
获取卷1(){
返回此.repCount;
}
});
如果(默认值){
$.extend(这是默认值);
}
}
$.extend(Set.prototype{
获取卷2(){
返回此.repCount;
}
});
var firstSet=新集合({
体重:135,
重复次数:8次
});
document.write(JSON.stringify(firstSet));
文件。写(“
”);
文件编写(第一套,第2卷);
以下是工作代码:
undefined = ''[0];
function Set(weight, repCount) {
this.weight = weight;
this.repCount = repCount;
$.extend(this, { //it using the current instance as the return
get volume1() {
return repCount * weight;
}
});
}
$.extend(Set.prototype, {
get volume2() {
return this.weight;
}
});
var firstSet = new Set(135, 8);
document.write(JSON.stringify(firstSet));
document.write("<br />");
document.write(firstSet.volume1);
undefined=''[0];
功能集(重量、重复计数){
重量=重量;
this.repCount=repCount;
$.extend(这个,{//)使用当前实例作为返回值
获取卷1(){
返回repCount*重量;
}
});
}
$.extend(Set.prototype{
获取卷2(){
返回此值。重量;
}
});
var firstSet=新的集合(135,8);
document.write(JSON.stringify(firstSet));
文件。写(“
”);
文件编写(第一套,第1卷);
进行了一些调试,我看到您的getter方法使用“undefined”实例在构造函数中将这两个实例相乘。所以undefined*undefined将返回NaN。我改变了脚本构造Set对象的方式以使其工作,它现在返回一个数字作为构造函数中对象集的属性,并且没有默认值undefined
通过设置document.write以获取卷1,更改getter以返回this.repCount,并将repCount设置为0或未定义,您可以看到它正在执行我所描述的操作:
undefined = ''[0];
function Set(defaults) {
$.extend(this, {
repCount: 0,
weight: 0,
get volume1() {
return this.repCount;
}
});
if (defaults) {
$.extend(this, defaults);
}
}
$.extend(Set.prototype, {
get volume2() {
return this.repCount;
}
});
var firstSet = new Set({
weight: 135,
repCount: 8
});
document.write(JSON.stringify(firstSet));
document.write("<br />");
document.write(firstSet.volume2);
undefined=''[0];
函数集(默认值){
$。扩展(此{
重复次数:0,
重量:0,,
获取卷1(){
返回此.repCount;
}
});
如果(默认值){
$.extend(这是默认值);
}
}
$.extend(Set.prototype{
获取卷2(){
返回此.repCount;
}
});
var firstSet=新集合({
体重:135,
重复次数:8次
});
document.write(JSON.stringify(firstSet));
文件。写(“
”);
文件编写(第一套,第2卷);
以下是工作代码:
undefined = ''[0];
function Set(weight, repCount) {
this.weight = weight;
this.repCount = repCount;
$.extend(this, { //it using the current instance as the return
get volume1() {
return repCount * weight;
}
});
}
$.extend(Set.prototype, {
get volume2() {
return this.weight;
}
});
var firstSet = new Set(135, 8);
document.write(JSON.stringify(firstSet));
document.write("<br />");
document.write(firstSet.volume1);
undefined=''[0];
功能集(重量、重复计数){
重量=重量;
this.repCount=repCount;
$.extend(这个,{//)使用当前实例作为返回值
获取卷1(){
返回repCount*重量;
}
});
}
$.extend(Set.prototype{
获取卷2(){
返回此值。重量;
}
});
var firstSet=新的集合(135,8);
document.write(JSON.stringify(firstSet));
文件。写(“
”);
文件编写(第一套,第1卷);
正如@pkyeck提到的,jQu