Skip to content

Instantly share code, notes, and snippets.

@h4
Created March 27, 2012 13:27
Show Gist options
  • Save h4/2215866 to your computer and use it in GitHub Desktop.
Save h4/2215866 to your computer and use it in GitHub Desktop.
JS. Создание объектов
// 1. Создание объекта
var person = new Object();
person.firstname = "Анна";
person.lastname = "Смирнова";
alert(person.lastname);
// 2. Литеральная форма записи
var my_obj = {
x: 5,
y: 4,
z: [
"a",
"b"
]
};
// 3. Конструктор объекта
function newlastname(new_lastname) {
this.lastname = new_lastname;
}
function Person(fname, lname, vozrast, color) {
this.firstname = fname;
this.lastname = lname;
this.age = vozrast;
this.eyecolor = color;
this.newlastname = newlastname;
}
myFather = new Person("Александр", "Смирнов", 50, "синий");
myMother = new Person("Анна", "Петрова", 48, "зеленый");
myMother.newlastname("Смирнова");
// 4. Метод объекта
function showBrowser() {
document.write("Обозреватель: " + this.name + " " + this.version);
}
function Browser(name, version) {
this.name = name;
this.version = version;
this.aboutBrowser = showBrowser;
}
var myBrowser = new Browser(IE, 8);
myBrowser.aboutBrowser();
// 5. Прототип объекта
function Employee(name, jobtitle, born) {
this.name = name;
this.jobtitle = jobtitle;
this.born = born;
}
var andy = new Employee("Андрей Николаев","врач",1970);
document.write(andy.salary);
Employee.prototype.salary=50000;
document.write(andy.salary);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment