Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Methods and properties.

Javascript is so flexible that we are able to attach and detach properties and methods even after the object creation. This makes the language extremely powerful as it offers us a possibility of modifying the behaviour of an object at runtime.Lets consider the following example:

<script type="text/javascript"> //--creation of the basic object var person = {name: "John",surname: "Doe",}; //--attaching a new property person.age = 32; //--attaching a new method. person.SaySomething = function() { console.write("Hello");};

</script>

as you have seen, adding the age property is as simple as declaring person.age and assigning a value. The same happens with the method declaration person.SaySomething that now points to the anonymous function that in our case simply prints “Hello” in the console.

Deleting a property from an object.Javascript offers another very interesting operation which is the possibility of deleting/detaching a property or a method from an already defined object. We can do this by using the delete keyword as follows:

<script type="text/javascript"> var person = {: "John",surname: "Doe",}; //--attaching a new method. person.SaySomething = function() { console.write("Hello");};   delete person.name; delete person.surname; delete person.SaySomething; </script>

Determine Whether an Object Has a Property.Another very handy operation that Javascript offers is the possibility of querying an already defined object whether it contains a property or a method. This could be considered some sort of Javascript “reflection” mechanism, as we know it in C#.
In order to determine if an object has a property or a method the in operation has to be used. Obviously we may use this feature to check if the property has been defined before attaching it to the object.

<script type="text/javascript"> var person = {name: "John",surname: "Doe",}; var hasName = "name" in person; var hasSurname = "surname" in person;   console.log("Has name: " + hasName); console.log("Has surname: " + hasSurname); </script>

Read/Write properties.Every time we declare a property, the javascript runtime automatically creates a getter and a setter, which means that we may read and/or assign values. There are mainly two ways of achieving this. By using the “standard” notation or the litteral notation as shown in the example below:

<script type="text/javascript"> var person = {name: "John", surname: "Doe",}; person.name = "Andrew"; person["surname"] = "Wooley" console.log(person["surname"]); console.log(person.name); </script>

 

Line,numbers

The String(or a line) object is used to manipulate a stored piece of text.String objects are created with new String().



Syntax: var txt = new String("string");

or more simply: var txt = "string";

The Numberobject is an object wrapper for primitive numeric values.Number objects are created with new Number().

Syntax: var num = new Number(value);

If the value parameter cannot be converted into a number, it returns NaN (Not-a-Number


Date: 2016-01-03; view: 809


<== previous page | next page ==>
Directive break and continue | Number Object Properties
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.039 sec.)