English 中文(简体)
javascript function tosimulate groups, Best practices?
原标题:javascript functions to simulate classes, best practices?

我注意到,我可以写像以下一类的职能,以模拟课堂。 我想知道,这是否是这样做的最新方式。 赞赏任何意见。 增 编

function Apple(type) {
    this.type = type;
    this.color = "red";
    this.getInfo = function () {
        return this.color +     + this.type +   apple ;
    };
}

var apple = new Apple( testapple );
apple.color =  blue ;
console.log(apple.getInfo());
最佳回答

您的法典对每个案例都做了细微的工作,但效果不够。 信息功能。 这一点可以避免。 你可以利用以下模式模拟联合材料中的班级。

Basic Pattern

为了模拟 阶级财产/方法,你将财产/方法设定在反应堆功能上。

function Apple() {};
Apple.classProperty = some_value;
Apple.classMethod = some_method;

为了模拟instance property,你在调解员职能范围内(如你在法典中所做的那样):

function Apple() {
  this.property = some_instance_value;
};

为了模拟instance methods,你在上设立了职能。 形形形色/代码

function Apple() {};
Apple.prototype.instanceMethod = function () {...};

Advanced Pattern

如果你想要制定私人/优先方法,。 现有非常有用的模式。

私人方法——只能供建筑商使用:

function Constructor(...) {
var that = this;
var membername = value;
function membername(...) {...}

}

Privileged Method - can access private method and is accesible to the public:

function Constructor(...) {
this.membername = function (...) {...};
}
问题回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.