I need a good explanation (references
are a plus) on Python's slice notation.
To me,
this notation needs a bit of picking up.
It
looks extremely powerful, but I haven't quite got my head around
it.
In href="http://www.imdb.com/title/tt2170049/" rel="nofollow">True Blood: Authority
Always Wins, Alexander Drew's character was great and fantastic, he was aged 9
years before being turned (according to href="http://trueblood.wikia.com/wiki/Alexander_Drew"
rel="nofollow">trueblood.wikia). But it also says that he serves the
Vampire Authority as a
Chancellor.
According to href="http://trueblood.wikia.com/wiki/Alexander_Drew"
rel="nofollow">trueblood.wikia:
Alexander Drew is an angelic looking, yet impetuous vampire. He was
turned at the tender age of 9, and serves the Vampire Authority as a
Chancellor. He enjoys smoking cigarettes and taunting everyone around
him,
including his fellow
Chancellors.
Other vampires
such as rel="nofollow">Salome and href="http://trueblood.wikia.com/wiki/Godric" rel="nofollow">Godric are
aged 2000+ years. So, how does it possible for Alexander to become a
chancellor of the Vampire authority?
Answer
href="http://trueblood.wikia.com/wiki/Alexander_Drew" rel="nofollow">As the wiki
states, Alexander was aged 9 years old when he was
turned.
Just like all vampires, they do not
age beyond what age they were when turned. For all we know he could be beyond age of all
vampires including Russell (2800 years old), Eric (1100+), and especially Bill (176).
This detail has not been made known yet, however but we
can assume he is a very old vampire, if not, very experienced to be on the
council.
How can I create static variables in
Javascript?
If you
come from a class-based, statically typed object-oriented language (like Java,
C++ or C#) I assume that you are trying to create a variable or method
associated to a "type" but not to an
instance.
An example using a "classical"
approach, with constructor functions maybe could help you to catch the concepts of basic
OO JavaScript:
function
MyClass () { // constructor function
var privateVariable = "foo"; // Private
variable
this.publicVariable = "bar"; // Public variable
this.privilegedMethod = function () { // Public Method
alert(privateVariable);
};
}
//
Instance method will be available to all instances but only load once in memory
MyClass.prototype.publicMethod = function () {
alert(this.publicVariable);
};
// Static variable shared
by all instances
MyClass.staticProperty = "baz";
var
myInstance = new
MyClass();
staticProperty
is defined in the MyClass object (which is a function) and has nothing to do with its
created instances, JavaScript treats functions as href="http://en.wikipedia.org/wiki/First-class_function" rel="nofollow
noreferrer">first-class objects, so being an object, you can assign
properties to a
function.
UPDATE:
ES6 introduced the ability to href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"
rel="nofollow noreferrer">declare classes through the
class keyword. It is syntax sugar over the existing
prototype-based inheritance.
The href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static"
rel="nofollow noreferrer">static keyword allows
you to easily define static properties or methods in a
class.
Let's see the above example implemented
with ES6 classes:
class MyClass {
// class constructor,
equivalent to
// the function body of a constructor
constructor()
{
const privateVariable = 'private value'; // Private variable at the
constructor scope
this.publicVariable = 'public value'; // Public
property
this.privilegedMethod = function() {
// Public
Method with access to the constructor scope variables
console.log(privateVariable);
};
}
//
Prototype methods:
publicMethod() {
console.log(this.publicVariable);
}
// Static
properties shared by all instances
static staticProperty = 'static
value';
static staticMethod() {
console.log(this.staticProperty);
}
}
// We
can add properties to the class prototype
MyClass.prototype.additionalMethod =
function() {
console.log(this.publicVariable);
};
var
myInstance = new MyClass();
myInstance.publicMethod(); // "public
value"
myInstance.additionalMethod(); // "public
value"
myInstance.privilegedMethod(); // "private
value"
MyClass.staticMethod(); // "static
value"First, it's not a good practice to name classes with a
lowercase, so try renaming to something like MakeGUI instead of
makeGUI.
The problem with your
commented code is that it creates a new instance of makeGUI
every time the button is clicked and the action listener is
invoked. The result is that when you click on the close button, a new frame is created,
then an inner one and this inner one gets immediately closed. The only thing you'd be
doing is creating more and more frames. You should keep the instance as a state, for
instance as a class member:
class MakeGUI
{
JFrame smallframe;
JButton close = new JButton("CLOSE
ME");
MakeGUI() {
frame f1 = new frame();
smallframe =
new JFrame(); //want to close this one
JPanel jp = new JPanel(new
FlowLayout());
smallframe.setSize(300, 300);
smallframe.setLocationRelativeTo(null);
smallframe.setDefaultCloseOperation(smallframe.DISPOSE_ON_CLOSE);
close.addActionListener(new action());
jp.add(close);
smallframe.add(jp);
smallframe.setVisible(true);
}
class action implements ActionListener {
public void
actionPerformed(ActionEvent e) {
if (e.getSource() == close) {
//
use this instead of dispose
smallframe.dispatchEvent(new
WindowEvent(smallframe, WindowEvent.WINDOW_CLOSING));
System.out.println("gotcha");
}
}
}
}