![]() 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> 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:
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#.
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:
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: 984
|