I just recently found out about jslint, a Javascript library for Javascript parsing.
Made a JS checker for the Excalibur Development Team:
I hope you guys find it useful.
I just recently found out about jslint, a Javascript library for Javascript parsing.
Made a JS checker for the Excalibur Development Team:
I hope you guys find it useful.
Once more, I’ll write a few words on virtual functions, as they are often confusing for myself. In C++ they exhibit an interesting behavior (by definition!) when casting from one class to another, that can be confusing if one isn’t very fond of the properties of a virtual function. In particular, in C++, virtual functions inside objects behave as being of the type they have been instantiated as, not as the types they may be converted to (by casting) throughout your code.
Take the following example:
#include <iostream> using namespace std; class A { public: virtual void Run() { cout << "A::Run" << endl; } void Yoke() { cout << "A::Yoke" << endl; } }; class B : private A { public: virtual void Run() { cout << "B::Run" << endl; } void Yoke() { cout << "B::Yoke" << endl; } };
Now, let us try to use classes A and B normally:
int main() { A* a = new A(); B* b = new B(); a->Run(); b->Run(); return 0; }
As one would expect, the result is A::Run and B::Run, normally. Function “Run” from class B overloads function “Run” inside class A, hence this behavior. Now let’s see what happens when we cast from one class to another.
int main() { A* a = new A(); B* b = ( B* )a; b->Run(); return 0; }
Calling the “Run” function, perhaps surprisingly, invokes the Run method of class A and outputs “A::Run()”. Although the type of variable b is a B*, in truth it is an instance of class A. What virtual functions are supposed to do is, lookup the real object type (meaning: the class with which they were instantiated), and call THAT function. The “b” variable was instantiated as A and hence this behavior.
This behavior is often desired. It is used when a parent class wants to call a function that can be overloaded; in that case, all the developer needs to do is instantiate the object using the child class. Take the following example, where this behavior helps:
class Foo { void Go() { cout << "Foo::Go" << endl; this->Run(); } virtual void Run() { cout << "Running as Foo" << endl; } } class Bar : private Foo { void Run() { cout << "Running as Bar" << endl; } } int main() { Bar* bar = Bar(); bar->Go(); }
While class Bar offers a generalized behavior in function Go, it allows specialization, if the derived class desires, by making its Run function virtual. Notice that the proper function is invoked because the instantiation is done as Bar, not as Foo, even though the Go function is defined within Foo.
Sometimes, however, this behavior is not desired. Take, for the sake of illustration, as an example, a base class which is instantiated by a library that is unaware of possible class extensions, and returned by one of its functions or passed as an argument to a user-defined callback function. Having that type of object, the user might desire to cast it to a different type, a derived class, which they could want to be specialized, by defining this special behavior in an overloaded function called by the parent.
In some cases, it is possible to solve this problem by simply making functions non-virtual. This allows the program to execute the functions based on variable types (casted or defined types), and not actual object types (instantiated type). Yet another way to do it is to define a constructor for the derived class that accepts the base class as an argument and instantiates accordingly. The latter method might not always be possible, however, depending on the context.
I hope this post made some things about virtual functions clearer
At least this is what internet browsers think.. or ok, not exactly.
Let me explain:
If you have an element which has onmouseover and onmouseout events, and children inside it, if you enter one of the children, onmouseout of parent element will fire, then the onmouseover of the child, and then again the onmouseout of the parent.
Example:
<body> <div style="width: 500px; height: 500px; background-color: red;" onmouseout="alert( 'leaving parent' );" onmouseover="alert( 'entering parent' );" id="parent"> <div style="width: 100px; height: 100px; background-color: blue;" onmouseover="alert( 'entering child' );" onmouseout="alert( 'leaving child' );" id="child"> </div> </div> </body>
If you enter the parent div, you will get an alert saying: “Entering parent” (obviously). But if you then enter the child-div,
you will get these alerts (consecutively):
“Leaving parent”, “Entering child”, “Entering parent”
The same thing happens if you then leave the child: you will get these alerts:
“Leaving child”, “Leaving parent”, “Entering parent”
Now, suppose that you have a <div> element,
which on mouse-over creates a <form> child and on mouse-out destroys it.
This won’t lead to the result you ‘d expect:
When you mouse-over it, it will create the <form>,
but instantly destroy it because you entered the child.
And then again, as you are not on the child,
you are re-entering the <div> and thus creating the <form> again.
…and so on.
Currently I’m working on converting my practice project, a simple uploader program in PHP, into OOP.
Let’s take a look at the construct of the User class. For every instance of the class that is created, there is a check on whether the parameter provided is an id or a username, simply by checking if it’s a string or an integer. If it’s an integer it supposes that the variable given is a user id, and so continues by filling up the other properties.
class User { private $mId; private $mName; ...... function __construct( $construct ) { if ( is_int( $construct ) ) { // mySQL query by user id $this->mId = $construct; ....... else { // mySQL query by username $this->mName = $construct; .......
In order to display the username which is a private property ($mName) I used a simple fuction
public function GetName() { return $this->mName; }
So in my index.php I used to following code to display the user who owned a specific folder:
$owner = New User( $folder_owner['id'] );
$folder_owner['id'] was part of an mySQL fetched array which carried the owner’s id.
However, when I called to my previous method to display the username:
echo $owner->GetName();
foo.txt, created on 12-06-2007 by 2
$owner = New User( $folder_owner['id'] );
The error was pointed out by my friend and tutor Dionyziz, who also suggested me the simple solution of converting $folder_owner['id'] to an integer before using it as a parameter for my method.
$owner = New User( ( integer ) $folder_owner['id'] );
What will definitely not be forgotten:
special thanks to Dionyziz for helping me out
More info:
We all agree that compiling errors are something very common while writing a program. Some of them can be detected by the compiler (mainly syntax errors) and some of them only during runtime. I thought the runtime errors were the most irritating and hard to detect ones until I got into this:
//graph.cpp #include <iostream> using namespace std; int main() { //----------Adjacency Matrix-------\\ int matrix[2][2]; matrix[0][0] = 0; matrix[0][1] = 1; matrix[1][0] = 1; matrix[1][1] = 1; //----------------------------------\\ // code was continued here.. return 0; }
g++ graph.cpp
graph.cpp: In function ‘int main()’:
graph.cpp:8: error: ‘matrix’ was not declared in this scope
I would probably think that g++ had some kind of problem or I would rewrite the code from the start if I didn’t recall that something similar had occured to abresas. In a perfectly written code the compiler produced strange errors. He had also told me that he solved the problem by removing the comments! We couldn’t understand what exactly was going back then, but there is always a simple logical explanation for everything.
Do you remember a rule that everyone should follow while writing macros? The macros should all be in one line. That would make the code ugly though, so the following code:
#define ASSERT(x)if(! (x) ) {cout << "ERROR! Assert " << #x << "failed\n";cout << " on line " << __LINE__ << '\n';cout << " in file " << __FILE__ << '\n';}
could also be written in this way:
#define ASSERT(x)\ if(! (x) ) {\ cout << "ERROR! Assert " << #x << "failed\n";\ cout << " on line " << __LINE__ << '\n';\ cout << " in file " << __FILE__ << '\n';\ }
Apparently, ‘\’ is used for connecting lines apart from being an escape character.Therefore:
int main() { //----------Adjacency Matrix-------\\ int matrix[2][2]; matrix[0][0] = 0;
is interpreted by the compiler as:
int main() { //----------Adjacency Matrix-------\\int matrix[2][2]; matrix[0][0] = 0;
and thus leaves us with an undefined variable.
Imagine having to face such an error while being on a contest. Yikes!
Most people use SSH to access their server over the Internet and administrate it. SSH is claimed to be a secure way of accessing a server. But with the wrong configuration this can be the contrary.
Disabling the direct root login is good as a hacker needs to guess 2 seperate passwords. So if he guesses the password of your first account he only has restricted access to your machine and needs to ‘su’ to gain root access, too.
First of all you should choose a good password for your users.
1. Let’s SSH into your server. You should use a second user account and ‘su’ to gain root access.
2. Open /etc/ssh/sshd_config with the editor of your choice.
vi /etc/ssh/sshd_config
3. For more security we should disable the insecure SSH Protocol 1. For doing so find the line ‘Protocol 2, 1′ and change it to ‘Protocol 2′.
4. Next thing is disabling the direct root login. The line ‘PermitRootLogin yes’ is allowing direct root logins. We need to change it to ‘PermitRootLogin no’.
5. After making these changes save the file and quit your editor.
6. Now we need to restart the SSHd
/etc/rc.d/init.d/sshd restart
Attention:
If you make a mistake in your sshd_config file it can happen that the SSHd will not start again. So take care of what you are doing and don’t logout before you tried your changes. Else you’ll need someone with physically access to your server to repair SSHd’s config.
Not long ago, as I walked through the valley of the shadow of DOM, trying to help a friend of mine in a script he was writing, I found out that many elements had more children nodes than I thought they should have. For example:
<html> <head> <title>Kamibu</title> </head> <body> <i> <b>a</b> </i> </body> </html>
From the first look it seems like the ‘i’ element has only one child node, the ‘b’ element. Using the following code at my browser though:
alert(document.body.childNodes[1].childNodes.length);
the number 3 comes up! But how could it be? :S By trying to alert the nodeName of these children
alert(document.body.childNodes[1].childNodes[0].nodeName); alert(document.body.childNodes[1].childNodes[1].nodeName); alert(document.body.childNodes[1].childNodes[2].nodeName);
I got:
#text
B
#text
And the thing is getting even more strange when the following code returns empty pop-ups:
alert(document.body.childNodes[1].childNodes[0].nodeValue); alert(document.body.childNodes[1].childNodes[2].nodeValue);
After a short discussion with dionyziz about this subject, he explained to me that the empty textNodes were actually the spaces that existed in the source code for it to be easily readable. Therefore, the following pieces of code are different:
<i> <b>a</b> </i>
<i><b>a</b></i>
This may result in unexplainable scripting errors when someone is trying to access a child element of a node and instead he accesses a blank text Node. The following short function can be used in order to determine whether a node is a textNode or not:
<script language='text/javascript'> function isTextNode(node) { if(node.nodeType == 3) { return true; } return false; } </script>
Recently, Izual and I were experimenting with DOM in order to add some more eye-candy to Chit-Chat, and experienced a mistake that often causes browser incompatibilities, but is looked over without paying much attention:
The nodeName attribute of a DOM element is not always reported in the case that it appears in your document. According to XHTML, all tags should be in lower-case; however, Internet Explorer 7, even if your tags are in lower case, returns the nodeName in upper case. This happens even when the rendering is done in XML strict and XHTML strict mode, as close as IE7 can get to that.
Take the following example:
<html>
<head>
<title>Sample</title>
</head>
<body>
<a href="" onclick="alert(document.body.nodeName); return false;">
Click me
</a>
</body>
</html>
While in Firefox the nodeName attribute value reported is “body”, Internet Explorer 7 reports “BODY”. This causes particular problems when comparing nodeName in order to detect a particular node. While it is more common to use .getElementsByTagName, we often need to compare tags one-by-one to avoid checking indirect children. Take the following example:
<html>
<head>
<title>Sample</title>
<script type="text/javascript">
function UpdateParagraph() {
var k = document.body.childNodes;
for ( i in k ) {
if ( k[ i ].nodeType == 1
&& k[ i ].nodeName == 'p' ) {
k[ i ].childNodes[ 0 ].nodeValue = 'Altered Paragraph!';
}
}
}
</script>
</head>
<body>
<p>Some paragraph</p>
<a href="" onclick="UpdateParagraph(); return false;">Click me</a>
</body>
</html>
In this case, Internet Explorer’s approach seems utterly problematic, as it will fail to recognize the tag. This can be easily fixed by converting the reported tag name to lower case:
if ( k[ i ].nodeType == 1 && k[ i ].nodeName.toLowerCase() == 'p' ) { k[ i ].childNodes[ 0 ].nodeValue = 'Altered Paragraph!'; }
Hey welcome to kamibu blog!
My name is Aleksis or “abresas”, and I’m honored to write the first post. Other members of the team:
Dionyziz – The big boss,
Izual – “Internet Explorer, I’m watching you”,
Kostis90gr – “I will goto..” and
Dimitris – aka Gatoni.
I am pretty excited about kamibu, and the things we are doing, but most of all excited about being member of such a team.
I’m sure everyone else is excited too!
Edit: Now we have 2 new members:
Makis – Doctor M.
and
Rhapsody – (bow), chinese not japanease