gpt4 book ai didi

javascript - 在模块模式内添加原型(prototype)方法 - 错误 : is not a function

转载 作者:行者123 更新时间:2023-12-03 09:05:03 25 4
gpt4 key购买 nike

我正在尝试使用模块模式进行简单的 JavaScript 练习,但还想集成原型(prototype)方法。即使在创建对象之后,在模块外部调用时也找不到原型(prototype)方法。我缺少什么?在模块模式中使用原型(prototype)方法是否违背了模块模式的目的?

var BroChart = (function(){
"use strict";

//revealing module pattern
return {
Point: Point,
Series: Series,
Chart: Chart,
addPoint: addPoint,
addPoints: addPoints,
addSeries: addSeries
};

function Point(x, y){
this.coordinates = [x, y];
}

function Series(label){
this.pointArray = [];
this.label = label;
}

// method to add single point
function addPoint(series, point) {

if (point instanceof Point) {
series.pointArray.push(point.coordinates);
} else {
console.log('Error: not a valid point');
}

}

// method to add array of points
function addPoints(series, points) {

points.forEach(function(point) {
if (point instanceof Point) {
series.pointArray.push(point.coordinates);
} else {
console.log('Error: not a valid point');
}
});

}


function Chart(title, data, type){
this.title = title;
this.data = data;
this.type = type;
this.printSeries = function() { _printSeries(this); };
}

//prototype method in question
Chart.prototype.printSeries2 = function() {
console.log(this.title + ' Chart');
console.log('Type: ' + this.type);
console.log('Data Points: ' + this.data.pointArray);
};

function addSeries(chart, series) {
if (series instanceof Series) {
chart.data.push(series.pointArray);
} else {
console.log('Error: not a valid series');
}
}

function _printSeries(chart) {
console.log(chart.title + ' Chart');
console.log('Type: ' + chart.type);
console.log('Data Points: ' + chart.data.pointArray);
}


})();

var broSeries = new BroChart.Series('Protein vs. Sick Gainz');
var firstPoint = new BroChart.Point (343, 21);
var secondPoint = new BroChart.Point (2, 11);
var thirdPoint = new BroChart.Point (54, 241);
var fourthPoint = new BroChart.Point (76, 988);

BroChart.addPoint(broSeries, firstPoint);
BroChart.addPoints(broSeries, [secondPoint, thirdPoint, fourthPoint]);

var Bro = new BroChart.Chart('Protein vs. Sick Gainz', broSeries, 'line');

Bro.printSeries(Bro);
//problematic prototype method call
Bro.printSeries2();

最佳答案

您应该在闭包末尾创建对象

var BroChart = (function(){
"use strict";

function Point(x, y){
this.coordinates = [x, y];
}

function Series(label){
this.pointArray = [];
this.label = label;
}

// method to add single point
function addPoint(series, point) {

if (point instanceof Point) {
series.pointArray.push(point.coordinates);
} else {
console.log('Error: not a valid point');
}

}

// method to add array of points
function addPoints(series, points) {

points.forEach(function(point) {
if (point instanceof Point) {
series.pointArray.push(point.coordinates);
} else {
console.log('Error: not a valid point');
}
});

}


function Chart(title, data, type){
this.title = title;
this.data = data;
this.type = type;
this.printSeries = function() { _printSeries(this); };
}

//prototype method in question
Chart.prototype.printSeries2 = function() {
console.log(this.title + ' Chart');
console.log('Type: ' + this.type);
console.log('Data Points: ' + this.data.pointArray);
};

function addSeries(chart, series) {
if (series instanceof Series) {
chart.data.push(series.pointArray);
} else {
console.log('Error: not a valid series');
}
}

function _printSeries(chart) {
console.log(chart.title + ' Chart');
console.log('Type: ' + chart.type);
console.log('Data Points: ' + chart.data.pointArray);
}

//revealing module pattern
return {
Point: Point,
Series: Series,
Chart: Chart,
addPoint: addPoint,
addPoints: addPoints,
addSeries: addSeries
};

})();

var broSeries = new BroChart.Series('Protein vs. Sick Gainz');
var firstPoint = new BroChart.Point (343, 21);
var secondPoint = new BroChart.Point (2, 11);
var thirdPoint = new BroChart.Point (54, 241);
var fourthPoint = new BroChart.Point (76, 988);

BroChart.addPoint(broSeries, firstPoint);
BroChart.addPoints(broSeries, [secondPoint, thirdPoint, fourthPoint]);

var Bro = new BroChart.Chart('Protein vs. Sick Gainz', broSeries, 'line');

Bro.printSeries(Bro);
//problematic prototype method call
Bro.printSeries2();

编辑:解释是 Chart.prototype.printSeries2 = function(){...} 是一条语句,由于顶部返回而未被执行。

在这里查看它的运行情况:https://jsfiddle.net/f8ednmw2/

关于javascript - 在模块模式内添加原型(prototype)方法 - 错误 : is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32209072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com