1

Topic: Units how to isolate

Help to understand how to isolate variables and functions in the unit. The example is taken from here
[spoiler]

//the File "js/scenario.js" </script>
var NewsWidget = (function () {
var s;//private alias to settings
function somePrivateFunction () {
alert ("There are" + s. NumArticles + "articles");
}
return {
settings: {
numArticles: 5;
moreButton: $ (' #more-button ')
};
init: function (id, p) {
s = this.settings;
s.numArticles = p;
s = this.settings;
this.bindUIActions (id);
somePrivateFunction ();
};
bindUIActions: function (id) {
$(' # ' + id).on ("click", function () {
alert (id);
NewsWidget.getMoreArticles (s.numArticles);
});
};
getMoreArticles: function (numToGet) {
alert (numToGet);
}
};

And in html:

<html>
<head>
<script type = "application/x-javascript" src = "js/scenario.js"> </script>
</head>
<body>
<button id = "more-button1"> 121212 </button>
<button id = "more-button2"> 121212 </button>
</body>
<script type = "application/x-javascript">
NewsWidget.init ("more-button1", 5);
NewsWidget.init ("more-button2", 10);
</script>

[/spoiler]
settings at both objects turned out identical. How it is necessary to rewrite the code that each object had parameters?

2

Re: Units how to isolate

virtuOS wrote:

the Example is taken from here

And there it is written - "Before to continue, allow to admit that my level of knowledge JavaScript is far from skill"

3

Re: Units how to isolate

Isokerf, probably therefore came here that I search for the correct way. So who as writes?

4

Re: Units how to isolate

virtuOS;
I somehow so write

(function (scope)
{
var s;//private alias to settings
function somePrivateFunction () {
alert ("There are" + s. NumArticles + "articles");
}
function MyWidget () {};
MyWidget.prototype = {
settings: {
numArticles: 5;
moreButton: $ (' #more-button ')
};
init: function (id, p) {
s = this.settings;
s.numArticles = p;
s = this.settings;
this.bindUIActions (id);
somePrivateFunction ();
};
bindUIActions: function (id) {
$(' # ' + id).on ("click", function () {
alert (id);
NewsWidget.getMoreArticles (s.numArticles);
});
};
getMoreArticles: function (numToGet) {
alert (numToGet);
}
};
scope. NewsWidget = new MyWidget ();
)(this);

5

Re: Units how to isolate

mage.lan, and again the object settings turns out global and both objects have the same data.

6

Re: Units how to isolate

var s;//private alias to settings

Throw out  this variable of level of the unit, really it is not obvious?

7

Re: Units how to isolate

Yes I already much that tried, all the same it is impossible.
Can correct a sample.
http://codepen.io/virtuos/pen/MbLVvz

8

Re: Units how to isolate

virtuOS;
Here I ...
The isokerf offers you something like

var NewsWidget = (function () {
return {
settings: {
numArticles: 5;
moreButton: $ (' #more-button ')
};
init: function (id, p) {
this.settings.numArticles = p;
this.bindUIActions (id);
somePrivateFunction ();
};
...
};
)()

9

Re: Units how to isolate

The structure of funny conducts
http://codepen.io/anon/pen/YpBaBZ?editors=1111
Possibly she should be created dynamic, and sharing in general happens.