14. var myObj = new Object();
myObj.name = "Fred";
myObj.age = 42;
myObj.getAge =
function () {
return this.age;
};
document.write(myObj.name);
document.write("<br/>");
document.write(myObj.age);
document.write("<br/>");
document.write(myObj.getAge());
// Output:
// Fred
// 42
// 42
仂亢仆仂 仂亰仆舒从仂仄亳 亳仄 于 仗亳仄亠亠 example-001.html
15. 仍亳 亳仄 于仂亶于舒 亳仍亳 仄亠仂亟舒 仆亠 磦仍磳 仗仂仄 亳亟亠仆亳亳从舒仂仂仄 亳仍亳
仆亠亳亰于亠仆仂 于 仄仂仄亠仆 仆舒仗亳舒仆亳 从亳仗舒, 仂 仄仂亢仆仂 亳仗仂仍亰仂于舒 于舒亢亠仆亳亠
于 从于舒亟舒仆 从仂弍从舒 亟仍 仂弍仂亰仆舒亠仆亳 于仂亶于舒. 亠亠亟 亟仂弍舒于仍亠仆亳亠仄 从
仂弍亠从 亳仄亠仆舒 于亠 于仂亶于 "expando" 于 JavaScript 仗亠仂弍舒亰ム 于 仂从亳.
var myObj = new Object();
// Add two expando properties that cannot be written in the
// object.property syntax.
// The first contains invalid characters (spaces), so must be
// written inside square brackets.
myObj["not a valid identifier"] = "This is the property value";
// The second expando name is a number, so it also must
// be placed inside square brackets
myObj[100] = "100";
18. // An array with three elements
var myArray = new Array(3);
// Add some data
myArray[0] = "Hello";
myArray[1] = 42;
myArray[2] = new Date(2000, 1, 1);
document.write("original length is: " + myArray.length);
document.write("<br/>");
// Add some expand properties
myArray.expando = "JavaScript!";
myArray["another Expando"] = "Windows";
// This will still display 3, since the two expando properties
// don't affect the length.
document.write("new length is : " + myArray.length);
// Output:
// original length is: 3
// new length is : 3
21. var x;
// This method works.
if (x == undefined) {
document.write("comparing x to undefined <br/>");
}
// This method works.
if (x == null) {
document.write("comparing x to null <br/>");
}
// This method doesn't work - you must check for the string "undefined".
if (typeof(x) == undefined) {
document.write("comparing the type of x to undefined <br/>");
}
// This method does work.
if (typeof(x) == "undefined") {
document.write("comparing the type of x to the string 'undefined'");
}
// Output:
// comparing x to undefined
// comparing x to null
// comparing the type of x to the string 'undefined'
undefined 亟仂于仂仍仆仂 舒仆仆亶
亰于亠亠从, 仍仂于仆仂 亞仂于仂, 仂仆仂
仂弍仂亰仆舒舒亠 仂 "亟舒仆仆 仆亠".
亠 null, 舒 亟舒仆仆 仆亠.
仂仆亳仄舒亶亠 从舒从 仂亳亠.
22. 1.8 亠仂弍舒亰仂于舒仆亳亠 亳仗仂于
亠仂弍舒亰仂于舒仆亳亠 亳仗舒 仄仂亢仆仂 磦仆仄 仂弍舒亰仂仄 亟亠仍舒 亠亠亰 亠亞仂
仆舒亰于舒仆亳亠:
var test = Boolean("something") // true
32. function getObjectQName(parameter1) {
var result = "My Name is: " + this.name;
if (parameter1 != null) {
result += ", and parameter1 is: " + parameter1;
}
return result;
}
getObjectQName.call(Eugene, "PAR-1"); // this === Eugene
getObjectQName.call(Illya, "PARAMETER-1"); // this === Illya
// Bind example
Eugene.illyaNameFnc = getObjectQName.bind(Illya);
writeLog(Eugene.illyaNameFnc("PAR-1")); // this === Illya
43. if (typeof(RiaShamans) == "undefined") RiaShamans = {};
if (typeof(RiaShamans.JSExamples) == "undefined") RiaShamans.JSExamples = {};
/**
* Class defines Person Entity
*
* @class {RiaShamans.JSExamples.Person}
* @param name
* @param age
* @param mass
* @returns {RiaShamans.JSExamples.Person}
*/
RiaShamans.JSExamples.Person = function (name, age, mass) {
this.initPerson(name, age, mass);
};
RiaShamans.JSExamples.Person.prototype.constructor = RiaShamans.JSExamples.Person;
/**
* Initialize person Data
*
* @param name
* @param age
* @param mass
*/
RiaShamans.JSExamples.Person.prototype.initPerson = function(name, age, mass){
this.name = name;
this.age = age;
this.mass = mass;
};
44. /**
* Returns Person name
*
* @returns {String}
*/
RiaShamans.JSExamples.Person.prototype.getName = function(){
return this.name;
};
/**
* Returns person Age
*
* @returns Number
*/
RiaShamans.JSExamples.Person.prototype.getAge = function(){
return this.age;
};
/**
* Class defines gendered Person Entity
*
* @class {RiaShamans.JSExamples.GenderPerson}
* @augments {RiaShamans.JSExamples.Person}
* @lends {RiaShamans.JSExamples.Person.prototype}
*/
RiaShamans.JSExamples.GenderPerson = function (name, age, mass) {
// Inherit parent constructor
RiaShamans.JSExamples.GenderPerson.superclass.constructor.call(this, name, age, mass); // this is equivalent of
RiaShamans.JSExamples.Person.call(this, name, age, mass);
this.gender = "UNKNOWN";
};
45. // Implement Inheritance
var GenderPersonPrototype = function(){};
GenderPersonPrototype.prototype = RiaShamans.JSExamples.Person.prototype; // Create prototype without parameters. We can't Instantiate Parent
without proper data in some cases.
RiaShamans.JSExamples.GenderPerson.prototype = new GenderPersonPrototype(); // Add all parent prototype data to Child class
RiaShamans.JSExamples.GenderPerson.prototype.constructor = RiaShamans.JSExamples.GenderPerson; // Set constructor
RiaShamans.JSExamples.GenderPerson.superclass = RiaShamans.JSExamples.Person.prototype; // Set superclass for Child
/**
* Returns Gender
*
* @returns {String}
*/
RiaShamans.JSExamples.GenderPerson.prototype.getGender = function(){
return this.gender;
};
/**
* Set Gender for a Person
*
* @param value
*/
RiaShamans.JSExamples.GenderPerson.prototype.setGender = function(value){
this.gender = value;
};