javascript OOP 中的公共方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14940845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:59:24  来源:igfitidea点击:

Public methods in javascript OOP

javascriptoopmethods

提问by Howie

I want to make a javascript class with methods which I can call within the class as well as outside of the class. I want to make a "public" method, if you will. I want getTextAreaElementand appendTextAreato be such methods.

我想使用可以在类内和类外调用的方法创建一个 javascript 类。如果您愿意,我想创建一个“公共”方法。我想要getTextAreaElement并且appendTextArea成为这样的方法。

I've shown a snippet of best code I could come up with so far. I've also tried defining the methods as prototypes as well as within the class (this.func = ...). But that only allowed me to call the method outside (new Socket().appendTextArea("osgjr89");) but NOTwithin the class itself! The code snippet below shows the exact opposite implementation where I can't call the method outside of the class but cancall it within.

我已经展示了迄今为止我能想出的最佳代码片段。我还尝试将方法定义为原型以及在类 ( this.func = ...) 中。但这仅允许我在 ( new Socket().appendTextArea("osgjr89");)之外调用该方法,而不能在类本身内调用!下面的代码片段显示了完全相反的实现,我不能在类外调用该方法,但可以在类内调用它。

Error:

错误:

Uncaught TypeError: Object #Socket has no method 'appendTextArea'

未捕获的类型错误:对象 #Socket 没有方法“appendTextArea”

socket.js:

套接字.js:

function Socket() {
var socket;
var canvas = document.getElementById('c');
var context = canvas.getContext("2d");

if (window.WebSocket) {
    socket = new WebSocket("ws://localhost:9012/websocket");
    socket.binaryType = 'arraybuffer';
    socket.onopen = onopen;
    socket.onmessage = onmessage;
    socket.onerror = onerror;
    socket.onclose = onclose;
} else {
    alert("Your browser does not support Web Socket.");
}

function getTextAreaElement() {
    return document.getElementById('responseText');
}

function appendTextArea(newData) {
    var el = getTextAreaElement();
    el.value = el.value + '\n' + newData + " :)";
}

function onopen(event) {
    getTextAreaElement().value = "Web Socket opened!"; 
}
/*[...]*/
}

main.js (loads after socket.js)

main.js(在 socket.js 之后加载)

$(document).ready(function() {
var s = new Socket();
s.appendTextArea("osgjr89"); // ERROR!
});

UPDATED socket.js:

更新的 socket.js:

function Socket() {
[...]
if (window.WebSocket) {
    socket = new WebSocket("ws://localhost:9012/websocket");
    socket.binaryType = 'arraybuffer';
    socket.onopen = this.onopen;
    socket.onmessage = this.onmessage;
    socket.onerror = this.onerror;
    socket.onclose = this.onclose;
} else {
    alert("Your browser does not support Web Socket.");
}

this.getTextAreaElement = function() {
    return document.getElementById('responseText');
}

this.appendTextArea = function(newData) {
    var el = this.getTextAreaElement();
    el.value = el.value + '\n' + newData + " :)";
}

this.onopen = function(event) {
    this.getTextAreaElement().value = "Web Socket opened!";
}
[...]
}

回答by bfavaretto

All public methods must be declared as properties, not variables/functions. So, you have to change stuff like this:

所有公共方法都必须声明为属性,而不是变量/函数。所以,你必须改变这样的东西:

function getTextAreaElement() {
    return document.getElementById('responseText');
}

into

进入

this.getTextAreaElement = function() {
    return document.getElementById('responseText');
}

回答by Beat Richartz

If you do this.func = function() {}, you can call the function inside the Constructor (Socketin your case) using this.func()as well as outside using:

如果你这样做this.func = function() {},你可以调用构造函数内部的函数(Socket在你的情况下)使用this.func()以及外部使用:

var s = new Socket();
s.func();