Category Archives: javascript

Data and Accessor Properties

In order to define a property we use Object’s defineProperty method on a particular object. There is the value we want to initialize with, which is the passed in parameter of var firstName. The second writable simply means if we can write over it.

Primitives and Objects

Primitive types:

  • String
  • Number
  • Boolean
  • Undefined
  • Null
  • Symbol
  • Symbol is a primitive data type of JavaScript, along with string, number, boolean, null and undefined. It was introduced in ECMAScript 2015. Once you create a symbol, its value is kept private and for internal use.

Reference types. Objects:

  • Object
  • Array
  • Function
  • Date
  • RegExp

Example

“hello”.length

But how?

new String(“hello”).length;

notice the constructor

type of obj –> “object”

typeof String(“hello”) –> “string”

obj.valueOf(); –> “hello”
typeof obj.valueOf(); –> “string”

Only way to create object is by using new String. All others will give you a string.

Similary,

//notice the constructor

typeof num; –> “object”
typeof 10 –> “number”

typeof Number(10) –> “number”
typeof num.valueOf() –> “number”

remember that our num is an object

typeof sum; –> “number”
typeof num; –> “object”

Even though num was used in a numeric operation, it did not get converted to number.

Cannot assign properties and methods to Primitives

For example

If we change num to object, then we can assign property and methods:

Identity vs Equality aka (strict vs abstract)

=== is called Identity Operator compares value AND type.

== will do type conversion. Namely, it converts the type down to a comparable value and match that way.

Notice that true != “true”, I want to comment that by rule 5, since true is Boolean, it gets converted into a number.
Hence, 1 != “true” is true.

Similarly, remember that in “==”, from rule 5), booleans are converted to numbers first.

Null manipulation

Adding and removing elements

given

where subHeading is a h1 element with text.

We now look at how the DOM’s body property deals with moving elements around

appendChild

appends the subHeading element to the body, which puts it in the very last place. In the DOM, you will see the subHeading element right before the closing body tag.

insertBefore

puts the subHeading element in front of the heading element.

Notice the h1 in front of the header element

contains and removeChild

first we check to see if heading element exist, if we do, just remove it

Notice no more header

Swapping out

our subHeading element is the 0th index or first header we see. Let’s say we want to replace that with an italic element.

Thus, we create an italic element first by using createElement and giving it a tag name ‘i’, which stands for italic. We then assign text content of that italic element. Finally we use DOM’s body property and use replaceChild to replace the header element with our italic element.

Traversing DOM

code

js

You’d get the element by tag name which is header. Then you the childNodes list, which can contain any node types. This includes:

NodeType Description
1 Element Represents an element Element, Text, Comment,
2 Attr Represents an attribute Text, EntityReference
3 Text Represents textual content in an element or attribute None
…..
….
….

Hence that’s why we see type Element and Text in our results. Finally, we see the total number of nodes representations which is 5.

result:
header
[text, h1#mainHeading.heading, text, h2.heading.subHeading, text]
5

Now let’s traverse this list. We go through each node in the childNode property and list their attributes. We want to see what kind of node types they are. In our case, we see that its all node types 1 and 3.

We also say that if the node type is 1, we want it to print out its tag name

result:

———————–
elements.js:89 nodeType: 3
elements.js:89 nodeType: 1
elements.js:92 Its of nodeType 1! ….I am a H1 tag
elements.js:89 nodeType: 3
elements.js:89 nodeType: 1
elements.js:92 Its of nodeType 1! ….I am a H2 tag
elements.js:89 nodeType: 3
elements.js:97
———————–

Most of the time, we just want to see the children element nodes, thus you can use the property children. Hence some of the more common properties can you can use are childNodes, children, and also firstChild/lastChild and nextSibling/previousSibling.

Manipulating DOM

Changing element’s id

mainHeading is reference object. We change the property of the
reference object and it will reflect back on the page.

You can also see the results by console

result:

getting the class names

result: heading subHeading

adding/removing a new class to an Element

subHeading.classList.add(‘special’);

You will then see the special class added to the element referenced by subHeading variable.

subHeading.classList.remove(‘special’);

Tag Name

console.log(‘mainHeading.tagName: ‘ + mainHeading.tagName); // H1

innerHTML

Contents of the elements
mainHeading.innerHTML = “” + mainHeading.innerHTML + ““;

outerHTML

Contents of the elements including the element itself.

mainHeading.outerHTML = “

” + mainHeading.innerHTML + “

“;

setAttribute

You can use methods setAttribute to

js

In the html you will see the attribute set in the h1 tag:

Does this element have attribute x ?

Selecting elements using DOM

Document Object Model methods used for selecting elements on a web page

Elements are represented by objects which contains useful properties and methods for working with them.

getElementById

– get element by their id

Fastest and easiest way of selecting something from the page is by select using
the element associated with the element.

Notice we have id “mainHeading” in the h1 element.

We can use

result:

getElementsByClass

– Get collection of elements by class name.

Given,

We use

results:

[h1#mainHeading.heading, h2.heading.subHeading, mainHeading: h1#mainHeading.heading]

getElementsByTagName

html:

result:

[a, a]

getElementsByName

html:

js:

result:
[a]