Jump to content
Sign in to follow this  
thescientist

Javascript: Syntax, best practices, questions, etc

Recommended Posts

Hey guys, I was just wondering if I could pick some of your brains for a few moments. I'm trying to further advance my skill set in the funky world of Javascript, and I'm trying to evolve my coding though the use of best practices, good planning, clean coding, etc and I have just finished reading Douglas Crockford's book java script: The good Parts and am starting into John Resig's book the Javascript Ninja and has a few quick questions for ya. Thanks in advance for your time and consideration. 1) From the John Resig's website...http://ejohn.org/apps/learn/#10How would you say line 2?

function yell(n){  return n > 0 ? yell(n-1) + "a" : "hiy";}assert(yell(4)=="hiyaaaa", "Calling the function by itself comes naturally.");

2) In Douglas Crockford book, java script: The Good Parts, he refers to the operators === and !== as alternatives to == and !=.

“When comparing to any of the following values, always use the === or != operators, which do not do type coercion: 0 “” undefinded null false true”
Does that mean that is the only applicable situations for those operators, is when comparing the above values? While we're on the subject, how would you define type coercion?3) Also, he suggests these shorthand methods:
instead of (foo != 0), use (foo)

does that mean if foo has a value of anything (number, boolean, string) then you can just use (foo) and that resolves to true, if foo != 0?

instead of (foo == 0) use (!foo)

does this only apply when using numbers, or would this apply to false or an empty string as the value of foo? 3) Also, Douglas Crockford suggests against using ++ and -–. Would the appropriate alternative to say something like

(for i = 0; i <= 10; i++)

be

(for i = 0; i <= 10; i+=1)?

Thanks again for reading through this, I'm open to all your thoughts, comments, or suggestions. Does anyone have any recommendations on good books that better promote good coding practices, or good habits for developing code structure, scalability, employing DRY techniques, etc?Peace, have a good one! :)

Share this post


Link to post
Share on other sites
3) Also, he suggests these shorthand methods:
instead of (foo != 0), use (foo)

does that mean if foo has a value of anything (number, boolean, string) then you can just use (foo) and that resolves to true, if foo != 0?

instead of (foo == 0) use (!foo)

does this only apply when using numbers, or would this apply to false or an empty string as the value of foo? 4? :) ) Also, Douglas Crockford suggests against using ++ and -–. Would the appropriate alternative to say something like

(for i = 0; i <= 10; i++)

be

(for i = 0; i <= 10; i+=1)?

Thanks again for reading through this, I'm open to all your thoughts, comments, or suggestions. Does anyone have any recommendations on good books that better promote good coding practices, or good habits for developing code structure, scalability, employing DRY techniques, etc?Peace, have a good one! :)

I can't say about 1 and 2 but for 3 and 3 (4) :) I have a few words.Personally I only use (foo) when I'm testing a boolean value. I think it's much easier to read and understand what exactly it is your conditionals are looking for.As for the ++ thing, I guess that would be a matter of personal opinion. To me it just looks messy to do this: (for i = 0; i <= 10; i+=1)I really don't think that one way is easier or more efficient. (If I'm wrong please correct me :) ) Edited by jkloth

Share this post


Link to post
Share on other sites
Hey guys, I was just wondering if I could pick some of your brains for a few moments. I'm trying to further advance my skill set in the funky world of Javascript, and I'm trying to evolve my coding though the use of best practices, good planning, clean coding, etc and I have just finished reading Douglas Crockford's book java script: The good Parts and am starting into John Resig's book the Javascript Ninja and has a few quick questions for ya. Thanks in advance for your time and consideration. 1) From the John Resig's website...http://ejohn.org/apps/learn/#10How would you say line 2?
function yell(n){  return n > 0 ? yell(n-1) + "a" : "hiy";}assert(yell(4)=="hiyaaaa", "Calling the function by itself comes naturally.");

2) In Douglas Crockford book, java script: The Good Parts, he refers to the operators === and !== as alternatives to == and !=. Does that mean that is the only applicable situations for those operators, is when comparing the above values? While we're on the subject, how would you define type coercion?3) Also, he suggests these shorthand methods:

instead of (foo != 0), use (foo)

does that mean if foo has a value of anything (number, boolean, string) then you can just use (foo) and that resolves to true, if foo != 0?

instead of (foo == 0) use (!foo)

does this only apply when using numbers, or would this apply to false or an empty string as the value of foo? 3) Also, Douglas Crockford suggests against using ++ and -–. Would the appropriate alternative to say something like

(for i = 0; i <= 10; i++)

be

(for i = 0; i <= 10; i+=1)?

Thanks again for reading through this, I'm open to all your thoughts, comments, or suggestions. Does anyone have any recommendations on good books that better promote good coding practices, or good habits for developing code structure, scalability, employing DRY techniques, etc?Peace, have a good one! :)

2) When comparing any value to the exact type use === or !==. I like to think of the === operator as "exactly equal to". Could be a string, number or boolean. Doesn't matter.
// This is true.var num = 1;if (num === 1) {return true;}// this is not true. Because the number is a string, not an integer.// the IF statement is comparing an integer, but it's ignoring the string because it's not the same type.// with == you could get away with this.var num = '1';if (num === 1) {return true;}// this is true.var num = '1';if (num === '1') {return true;}

3) I find these a bit tricky. I like to use them only for booleans or things that have a value above zero. I find that the value 0 (zero) will return true for (!var). Sometimes I need "0" to be a real value though, so (!var) doesn't work for me then.

// this returns true even when "0" is the value.var num = 0;if (!num) {alert("returned true");}

I don't have any comments on the first and last one.

Share this post


Link to post
Share on other sites

1) "Return, if n is greater than 0, yell of n minus 1, plus a, or else 'hiy'." The actual construct is known as the ternary operator.2) The === and related operators are type-conscious. E.g. 1 == "1" but 1 !== "1". Use them when you need this distinction.3) Certain values == false (but don't === false, as we saw earlier). These include 0, 0.0, "" and []. Note that this is not true in all languages - for example, in Java only the boolean value false resolves to false.4) The postincrement and postdecrement operators, as they are known, are indeed equivalent to +=1 or -=1, when written as a statement by themselves. However, the postincrement etc. operators can be used inline as part of another statement, e.g. j = 5 * (i++) + 2;.

Share this post


Link to post
Share on other sites

I think the issue with increment operators is entirely psychological.1. developers might forget the difference between a post-increment and a pre-increment when embedding increments in a complex statement.The solution is to not use write code so terse that increments are embedded in complex statements. It is not 1969. We can waste a few bytes to make code more readable.2. developers might make mistakes if they use multiple statements in the 3rd section of a for-loop initializer, and somehow the different statements involve the same variable.The solution is to not do that. Again, we do not really need to save bytes, and there is almost never a good reason to include multiple statements in any segment of a for-loop initializer. Old-time C programmers may like to write really terse code, but it's really not necessary, especially in a scripting environment like JavaScript or PHP.

Share this post


Link to post
Share on other sites
for(;;e++)if(g)a=b?c++&f:--d

:)

Share this post


Link to post
Share on other sites

Not a bad example. You can see an immediate issue that might trap the unsuspecting developer. Let's add some statements prior to that mess:c=0;d=0;for(;;e++)if(g)a=b?c++&f:--d;What are the values of c and d when the ternary is first evaluated?(1) c == 0; d == 0(2) c == 1; d == -1(3) c == 0; d == -1(4) c == 1; d == 0

Share this post


Link to post
Share on other sites
Not a bad example. You can see an immediate issue that might trap the unsuspecting developer. Let's add some statements prior to that mess:c=0;d=0;for(;;e++)if(g)a=b?c++&f:--d;What are the values of c and d when the ternary is first evaluated?(1) c == 0; d == 0(2) c == 1; d == -1(3) c == 0; d == -1(4) c == 1; d == 0
Wow! I don't even know where to begin to try and understand what that's doing. :)

Share this post


Link to post
Share on other sites
Not a bad example. You can see an immediate issue that might trap the unsuspecting developer. Let's add some statements prior to that mess:c=0;d=0;for(;;e++)if(g)a=b?c++&f:--d;What are the values of c and d when the ternary is first evaluated?(1) c == 0; d == 0(2) c == 1; d == -1(3) c == 0; d == -1(4) c == 1; d == 0
Wow! I don't even know where to begin to try and understand what that's doing. :)
I'm still ashamed that I can't even count to 4. Forget JS, I need to go back to kindergarten... :)

Share this post


Link to post
Share on other sites
I'm still ashamed that I can't even count to 4. Forget JS, I need to go back to kindergarten... :)
:)I was going to tell you that you spelled 'kindergarten' wrong, that it's supposed to be 'kindergarden'. But then I decided to look it up before I posted and guess what...I was wrong! :)

Share this post


Link to post
Share on other sites

phew, I kinda thought twice when typing that out and maybe I should use another analogy because I didn't know if I was spelling it right :)

Share this post


Link to post
Share on other sites

 

hello sir i have one massage problem

i did  my blog this massage code but its not work i dint know why?

https://nitzteach01.blogspot.com/action_page.php?msg=hello

<!doctype html>

this is my blog side

when i did any massage after that 404 page coming on the page

 so where is wrong i don't know where is worn can you help me?

<style>
body {font-family: Arial, Helvetica, sans-serif;}
* {box-sizing: border-box;}

/* Button used to open the chat form - fixed at the bottom of the page */
.open-button {
  background-color: #555;
  color: white;
  padding: 16px 20px;
  border: none;
  cursor: pointer;
  opacity: 0.8;
  position: fixed;
  bottom: 23px;
  right: 28px;
  width: 280px;
}

/* The popup chat - hidden by default */
.chat-popup {
  display: none;
  position: fixed;
  bottom: 0;
  right: 15px;
  border: 3px solid #f1f1f1;
  z-index: 9;
}

/* Add styles to the form container */
.form-container {
  max-width: 300px;
  padding: 10px;
  background-color: white;
}

/* Full-width textarea */
.form-container textarea {
  width: 100%;
  padding: 15px;
  margin: 5px 0 22px 0;
  border: none;
  background: #f1f1f1;
  resize: none;
  min-height: 200px;
}

/* When the textarea gets focus, do something */
.form-container textarea:focus {
  background-color: #ddd;
  outline: none;
}

/* Set a style for the submit/send button */
.form-container .btn {
  background-color: #4CAF50;
  color: white;
  padding: 16px 20px;
  border: none;
  cursor: pointer;
  width: 100%;
  margin-bottom:10px;
  opacity: 0.8;
}

/* Add a red background color to the cancel button */
.form-container .cancel {
  background-color: red;
}

/* Add some hover effects to buttons */
.form-container .btn:hover, .open-button:hover {
  opacity: 1;
}
</style>

<h2>chat section</h2>
<p>when you like asked any question please say not to be hesitated  </p>

<button class="open-button" onclick="openForm()">Chat</button>

<div class="chat-popup" id="myForm">
  <form action="/action_page.php" class="form-container">
    <h1>Chat</h1>

    <label for="msg"><b>Message</b></label>
    <textarea placeholder="Type message.." name="msg" required></textarea>

    <button type="submit" class="btn">Send</button>
    <button type="button" class="btn cancel" onclick="closeForm()">Close</button>
  </form>
</div>

<script>
function openForm() {
  document.getElementById("myForm").style.display = "block";
}

function closeForm() {
  document.getElementById("myForm").style.display = "none";
}
</script>


</!doctype>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...