Kategori: Javascript och jQuery

Samma debug-knappar för xdebug och DevTools

Jag programmerar i PHP och använder PHP Storm med xdebug.
Jag programmerar även i Javascript och använder Chrominumbaserade webbläsare med dess inbyggda DevTools.

Det är olika knappar i PHP Storm <> DevTools för att göra samma sak.

TypXdebugChromeNotering
Step intoF7F11Stega igenom nästa funktionsanrop
Step out ofSHIFT + F8SHIFT + F11Lämna aktuell funktion
Step overF8F10Kör nästa funktionsanrop utan stegvis genomgång
RunF9F8Återuppta scriptkörning
(mer …)

JavaScript – The language without context.

The peanut You received a peanut. You do not have the context. There is no way you know why you received a peanut and what to do with it.If you feel hungry and I have peanuts, you ask me for a peanut, you get a peanut and you eat it. That is context.Lets say you want to do other stuff while you wait for a peanut. You want to surf on your phone. You feel hungry, you say ”I am hungry, can anyone give me a peanut”, and then you start surfing on your phone. I hear your message and I have peanuts, I give you a peanut. When you receive a peanut, then you eat it.Now you do not need to know the extended context. It has been reduced to: ”When you receive a peanut, then you eat it.”. I saw that you reacted this way and found it amusing, so I gave you another peanut, and another…The Chinese businessmen A group of Chinese businessmen have traveled to Sweden to look at a secret new prototype that the Chinese company are developing in Sweden. All information is in need to know basis.The Chinese businessmen have traveled far, they are tired and hungry. Their secret task is to measure the prototype and call a secret buyer and relay the information. If the buyer say OK then the prototype will be shipped to the buyer. The Chinese businessmen are met at the factory by a very polite person. - I am your personal contact person. I will always be at your side while you are here at the factory. The prototype is in the storage. If you give me instructions what to do with the prototype then I promise to execute them.The Chinese businessmen really needed the prototype so they said: - Your instructions are to bring the prototype back here. We will wait. - Yes, I understand.The contact person smiled and stood absolutely still.What can we learn from the peanut and the prototype? In JavaScript there is no way you can hide your context. If you try then you end up with a peanut in your hand and a smiling Swede at your site.(mer …)

Funktionsvariabler

Du kan skicka in variabler i funktioner i de flesta programmeringsspråk. Det kan se ut så här:

function MyFunction($myData1 = 123, $myData2 = 'Hello')
Om du skriver så här:
MyFunction(,'Hej');
så betyder det att du nöjer dig med standardvärdet på första variabeln, och sätter ett eget värde på den andra variabeln.Om du råkar blanda ihop det hela och skriver:
MyFunction('Hej', 456);
Då måste MyFunction göra en kontroll av varje variabel att den innehåller det som förväntats. Programmeringsspråk som har en inbyggd kontroll av variablernas datatyp kommer att protestera. Men PHP och Javascript har inte detta (PHP har en svag kontroll i senare versioner av PHP)Jag ska presentera ett förslag för dig så får du fundera på det. Det här förslaget fungerar i alla programmeringsspråk men jag ger dig ett exempel i PHP och i Javascript.(mer …)

GreaseMonkey

Och så har jag till slut upptäckt GreaseMonkey. Det är ett tillägg i webbläsaren som gör att du kan köra dina egna JavaScript-program på valfri hemsida. Använder du Facebook? Titta då på denna lista med scripts för Facebook så ser…

Användandet av goto i programkod

goto-jailNär man skriver programkod är det av yttersta vikt att det ska vara lättläst. Hellre lättläst än snabbt brukar jag arbeta efter. Ofta är det sällan någon större skillnad i snabbhet mellan kod man förstår och krånglig kod som ingen vågar röra.Den här artikeln handlar om kommandot ”goto” och dess dåliga rykte. Goto finns i språket PHP sedan v 5.3 men tyvärr inte i Javascript. Det är på inget sätt dåligt att använda kommandot goto. Alla kommandon kan användas på ett sätt som gör det svårare eller lättare att läsa programkoden.Här kommer ett exempel i Javascript som innehåller en massa if-kommandon.(mer …)

CharZam