Tanya  Shields

Tanya Shields

1594859340

JavaScript Tips — Child Constructors, Text Selection, Inline Workers

Like any kind of apps, there are difficult issues to solve when we write JavaScript apps.

In this article, we’ll look at some solutions to common JavaScript problems.

Prevent Text Selection After Double Click

We can stop text selection after a double click by calling preventDefault() to stop the default action.

For example, we can write:

document.addEventListener('mousedown', (event) => {
  if (event.detail > 1) {
    event.preventDefault();
    // ...
  }
}, false);

Why is it Necessary to Set the Prototype Constructor?

We’ve to set the prototype constructor so that we can check with instanceof that the prototype’s constructor is a given constructor.

For instance, if we have:

function Person(name) {
  this.name = name;
}  

function Student(name) {  
  Person.call(this, name);
}
Student.prototype = Object.create(Person.prototype);

Then the Student ‘s prototype is set to Person .

But we actually want to set it to Student , even though it inherits from Person .

Therefore, we need to write:

Student.prototype.constructor = Student;

Now if we create an instance of Student , and check if it with instanceof :

student instanceof Student

Then that would return true .

If we use the class syntax, then we don’t have to do that anymore.

We just write:

class Student extends Person {
}

and everything else is handled for us.

#javascript #technology #software-development #programming #web-development

What is GEEK

Buddha Community

JavaScript Tips — Child Constructors, Text Selection, Inline Workers

Navigating Between DOM Nodes in JavaScript

In the previous chapters you've learnt how to select individual elements on a web page. But there are many occasions where you need to access a child, parent or ancestor element. See the JavaScript DOM nodes chapter to understand the logical relationships between the nodes in a DOM tree.

DOM node provides several properties and methods that allow you to navigate or traverse through the tree structure of the DOM and make changes very easily. In the following section we will learn how to navigate up, down, and sideways in the DOM tree using JavaScript.

Accessing the Child Nodes

You can use the firstChild and lastChild properties of the DOM node to access the first and last direct child node of a node, respectively. If the node doesn't have any child element, it returns null.

Example

<div id="main">
    <h1 id="title">My Heading</h1>
    <p id="hint"><span>This is some text.</span></p>
</div>

<script>
var main = document.getElementById("main");
console.log(main.firstChild.nodeName); // Prints: #text

var hint = document.getElementById("hint");
console.log(hint.firstChild.nodeName); // Prints: SPAN
</script>

Note: The nodeName is a read-only property that returns the name of the current node as a string. For example, it returns the tag name for element node, #text for text node, #comment for comment node, #document for document node, and so on.

If you notice the above example, the nodeName of the first-child node of the main DIV element returns #text instead of H1. Because, whitespace such as spaces, tabs, newlines, etc. are valid characters and they form #text nodes and become a part of the DOM tree. Therefore, since the <div> tag contains a newline before the <h1> tag, so it will create a #text node.

To avoid the issue with firstChild and lastChild returning #text or #comment nodes, you could alternatively use the firstElementChild and lastElementChild properties to return only the first and last element node, respectively. But, it will not work in IE 9 and earlier.

Example

<div id="main">
    <h1 id="title">My Heading</h1>
    <p id="hint"><span>This is some text.</span></p>
</div>

<script>
var main = document.getElementById("main");
alert(main.firstElementChild.nodeName); // Outputs: H1
main.firstElementChild.style.color = "red";

var hint = document.getElementById("hint");
alert(hint.firstElementChild.nodeName); // Outputs: SPAN
hint.firstElementChild.style.color = "blue";
</script>

Similarly, you can use the childNodes property to access all child nodes of a given element, where the first child node is assigned index 0. Here's an example:

Example

<div id="main">
    <h1 id="title">My Heading</h1>
    <p id="hint"><span>This is some text.</span></p>
</div>

<script>
var main = document.getElementById("main");

// First check that the element has child nodes 
if(main.hasChildNodes()) {
    var nodes = main.childNodes;
    
    // Loop through node list and display node name
    for(var i = 0; i < nodes.length; i++) {
        alert(nodes[i].nodeName);
    }
}
</script>

The childNodes returns all child nodes, including non-element nodes like text and comment nodes. To get a collection of only elements, use children property instead.

Example

<div id="main">
    <h1 id="title">My Heading</h1>
    <p id="hint"><span>This is some text.</span></p>
</div>

<script>
var main = document.getElementById("main");

// First check that the element has child nodes 
if(main.hasChildNodes()) {
    var nodes = main.children;
    
    // Loop through node list and display node name
    for(var i = 0; i < nodes.length; i++) {
        alert(nodes[i].nodeName);
    }
}
</script>

#javascript 

Vincent Lab

Vincent Lab

1605177504

Text to Speech in Node.js

In this video, I will be showing you how to turn text into speech in Node.js

#javascript #text to speech #javascript api #text to speech app #node.js text to speech #javascript text to speech

Tanya  Shields

Tanya Shields

1594859340

JavaScript Tips — Child Constructors, Text Selection, Inline Workers

Like any kind of apps, there are difficult issues to solve when we write JavaScript apps.

In this article, we’ll look at some solutions to common JavaScript problems.

Prevent Text Selection After Double Click

We can stop text selection after a double click by calling preventDefault() to stop the default action.

For example, we can write:

document.addEventListener('mousedown', (event) => {
  if (event.detail > 1) {
    event.preventDefault();
    // ...
  }
}, false);

Why is it Necessary to Set the Prototype Constructor?

We’ve to set the prototype constructor so that we can check with instanceof that the prototype’s constructor is a given constructor.

For instance, if we have:

function Person(name) {
  this.name = name;
}  

function Student(name) {  
  Person.call(this, name);
}
Student.prototype = Object.create(Person.prototype);

Then the Student ‘s prototype is set to Person .

But we actually want to set it to Student , even though it inherits from Person .

Therefore, we need to write:

Student.prototype.constructor = Student;

Now if we create an instance of Student , and check if it with instanceof :

student instanceof Student

Then that would return true .

If we use the class syntax, then we don’t have to do that anymore.

We just write:

class Student extends Person {
}

and everything else is handled for us.

#javascript #technology #software-development #programming #web-development

Niraj Kafle

1589255577

The essential JavaScript concepts that you should understand

As a JavaScript developer of any level, you need to understand its foundational concepts and some of the new ideas that help us developing code. In this article, we are going to review 16 basic concepts. So without further ado, let’s get to it.

#javascript-interview #javascript-development #javascript-fundamental #javascript #javascript-tips

Lowa Alice

Lowa Alice

1624392000

JavaScript Constructor Functions. IN JUST 6 MINUTES!!!

JavaScript Constructor Functions made simple.

📺 The video in this post was made by Programming with Mosh
The origin of the article: https://www.youtube.com/watch?v=23AOrSN-wmI&list=PLTjRvDozrdlxEIuOBZkMAK5uiqp8rHUax&index=10
🔥 If you’re a beginner. I believe the article below will be useful to you ☞ What You Should Know Before Investing in Cryptocurrency - For Beginner
⭐ ⭐ ⭐The project is of interest to the community. Join to Get free ‘GEEK coin’ (GEEKCASH coin)!
☞ **-----CLICK HERE-----**⭐ ⭐ ⭐
Thanks for visiting and watching! Please don’t forget to leave a like, comment and share!

#javascript #functions #constructor #constructor functions #javascript constructor functions