get child elements inside div using javascript

blog1

JavaScript

 type="text/javascript">
    function getChilds()
    {
        var arr=document.getElementById('mess').childNodes, str='';
        for(var i=0; iarr.length; i++) {
            if(arr[i].innerHTML!=undefined) {
                str+=" "+(arr[i].textContent||arr[i].innerText);
            }
        }
        document.getElementById("divcontent").value=str;
        return true;
    }

Object.defineProperty() Javascript

There will be some situations where we should not allow our objects to be override or delete etc. Then we have to use this defineProperty.

The Object.defineProperty() method defines a new property directly on an object, or modifies an existing property on an object, and returns the object.

Syntax

Object.defineProperty(obj, prop, descriptor)

Parameters

obj
The object on which to define the property.
prop
The name of the property to be defined or modified.
descriptor
The descriptor for the property being defined or modified.

When do you use?

Object.defineProperty is mainly used to set properties with specific property descriptors (e.g. read-only (constants), enumerability (to not show a property in a for (.. in ..) loop, getters, setters).

In Javascript, the standard properties (data member with getter and setter described above) are defined by accessor descriptor (in the link you have in your question). Exclusively, you can use data descriptor (so you can’t use i.e. value and set on the same property):

  • accessor descriptor = get + set (see the example above)
    • get must be a function; its return value is used in reading the property; if not specified, the default is undefined, which behaves like a function that returns undefined
    • set must be a function; its parameter is filled with RHS in assigning a value to property; if not specified, the default is undefined, which behaves like an empty function
  • data descriptor = value + writable (see the example below)
    • value default undefined; if writable, configurable and enumerable (see below) are true, the property behaves like an ordinary data field
    • writable – default false; if not true, the property is read only; attempt to write is ignored without error*!

Both descriptors can have these members:

  • configurable – default false; if not true, the property can’t be deleted; attempt to delete is ignored without error*!
  • enumerable – default false; if true, it will be iterated in for(var i in theObject); if false, it will not be iterated, but it is still accessible as public

* unless in strict mode – in that case JS stops execution with TypeError unless it is caught in try-catch block

Example:

var createPerson = function(firstName,lastName){
var person = {};
Object.defineProperty(person,”firstName”,{
value:firstName,
writable:false
});
Object.defineProperty(person,”lastName”,{
value:lastName,
writable:true
});
return person;
};

var person = createPerson(“sathya”,”vakacharla”);

Instead you can use object.defineProperties to set properties for firstName and lastName at a time.,

Null Vs Undefined Javascript

In JavaScript, undefined means a variable has been declared but has not yet been assigned a value, such as:

 var TestVar;
 alert(TestVar); //shows undefined
 alert(typeof TestVar); //shows undefined

null is an assignment value. It can be assigned to a variable as a representation of no value:

 var TestVar = null;
 alert(TestVar); //shows null
 alert(typeof TestVar); //shows object

From the preceding examples, it is clear that undefined and null are two distinct types: undefinedis a type itself (undefined) while null is an object.

Unassigned variables are initialized by JavaScript with a default value of undefined. JavaScript never sets a value to null. That must be done programmatically.

 null === undefined // false
 null == undefined // true
 null === null // true

and

 null = 'value' // ReferenceError
 undefined = 'value' // 'value'

Function Vs Method Javascript

A function is a piece of code that is called by name. It can be passed data to operate on (ie. the parameters) and can optionally return data (the return value).

All data that is passed to a function is explicitly passed.

A method is a piece of code that is called by name that is associated with an object. In most respects it is identical to a function except for two key differences.

  1. It is implicitly passed for the object for which it was called.
  2. It is able to operate on data that is contained within the class (remembering that an object is an instance of a class – the class is the definition, the object is an instance of that data).