标签:方法 inf var writable == pac console tom property
面向对象(Object-Oriented, oo)语言的一个是标志--类的概念
//6.1理解对象
var person = new Object();
person.name = ‘nick‘;
person.age = ‘29‘;
person.job = ‘software engineer‘;
person.sayName = function() {
console.log(this.name)
}
//对象字面量
person.sayName()
var person = {
name: ‘nick1‘,
age: ‘30‘,
job: ‘software engineer2‘,
sayName: function() {
console.log(this.name)
}
}
person.sayName()
6.1.1 属性类型
var person = {}
Object.defineProperty(person, ‘name‘, {
writable: false, //是否修改属性的值
value: ‘nich‘
})
console.log(person.name);
person.name = ‘greg‘
console.log(person.name);
var person = {}
Object.defineProperty(person, ‘name‘, {
writable: true,
configurable: false,
value: ‘nichol‘
})
console.log(person.name);
delete person.name
// person.name = ‘greg‘
console.log(person.name);
var person = {}
Object.defineProperty(person, ‘name‘, {
// writable: true,
configurable: true, ///
value: ‘nichol‘
})
console.log(person.name);
delete person.name
// person.name = ‘greg‘
console.log(person.name);
//访问器属性
var book = {
_year: 2004,
edition: 1
}
Object.defineProperty(book, ‘year‘, {
get: function() {
return this._year
},
set: function(newValue) {
if (newValue > 2004) {
this._year = newValue;
this.edition += newValue - 2004
}
}
})
book.year = 2006
console.log(book, book.edition)
//6.2.1 工厂模式 13:04
function createPerson(name, age, job) {
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
return o;
}
var person1 = createPerson("taozi", "24", "dog");
var person2 = createPerson("dahua", "25", "worker");
console.log(person1, person2)
//使用场比如登陆模块需要不同的单中类型的弹框
//需要一个错误警示框
var LoginAlert = function(text) {
this.content = text
}
LoginAlert.prototype.show = function() {
console.log(‘print-----LoginAlert‘)
}
var userNameAlert = new LoginAlert(‘用户名不能超过16数字后者字母‘)
userNameAlert.show();
//使用场比如登陆模块需要一个确认框
var LoginConfirm = function(text) {
this.content = text
}
LoginConfirm.prototype.show = function() {
console.log(‘print-----LoginConfirm‘)
}
var LoginFailConfirm = new LoginConfirm(‘你输入的用户名不存在‘)
LoginFailConfirm.show();
//使用场比如登陆模块需要一个Prompt
var LoginPrompt = function(text) {
this.content = text
}
LoginPrompt.prototype.show = function() {
console.log(‘print-----LoginPrompt‘)
}
var LoginPrompt = new LoginPrompt(‘你输入的LoginPrompt‘)
LoginPrompt.show();
//三个弹框的共同点:都有text文案都有一个show方法,知识类型不同而已,可以定义一个type在show不同type现实不同show
function createPop(type, text) {
var o = new Object();
o.content = text;
o.show = function() {
//显示方法
if (type == ‘alert‘) {
//alert
console.log(‘工厂模式---alert‘)
}
if (type == ‘promt‘) {
//promt
console.log(‘工厂模式---promt‘)
}
if (type == ‘confirm‘) {
//confirm
console.log(‘工厂模式---confirm‘)
}
}
return o;
}
var userNameAlert2 = new createPop(‘alert‘, ‘用户名不能超过16数字字母‘)
userNameAlert2.show()
标签:方法 inf var writable == pac console tom property
原文地址:https://www.cnblogs.com/pikachuworld/p/10889749.html