1.对象适合于收集和管理数据,容易形成树型结构。 Javascript包括一个原型链特性,允许对象继承另一对象的属性。正确的使用它能减少对象的初始化时间和内存消耗。 2.函数它们是javascript的基础模块单元,用于代码复用、信息隐藏和组合调用。函数用于指定对象的行为。一般来说,编程就是将一组需求分解成一组函数和数据结构的技能。 3.模块我们可以使用函数和闭包来构造模块。模块是一个提供接口却隐藏实现状态和实现的函数或对象。
function Person (name, age, job) {
// 实例变量
this.name = name;
this.age = age;
this.job = job;
}
Person.prototype.sayName = function () {
alert(this.name);
}
var person1 = new Person('Nicholas', 29, 'Software Engineer');
var person2 = new Person('Greg', 27, 'Doctor');
function SuperType (name) {
this.name = name;
this.colors = ['red','blue', 'green'];
}
SuperType.prototype.sayName = function () {
console.log(this.name);
}
function SubType (name, age) {
// 继承属性
SuperType.call(this,name);
this.age = age;
}
// 继承方法
SubType.prototype = new SuperType();
SubType.prototype.sayAge = function () {
console.log(this.age)
}
var instance1 = new SubType('Nicholas', 29);
instance1.colors.push('black')
console.log(instance1.colors);
instance1.sayName();
instance1.sayAge();
var instance2 = new SubType('Greg', 27)
console.log(instance2.colors);
instance2.sayName();
instance2.sayAge();
function inheritPrototype (subType,superType) {
var prototype = Object.creat(superType.prototype);
prototype.constructor = subType;
subType.prototype = prototype;
};
function SuperType (name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
SuperType.prototype.sayName = function () {
console.log(this.name);
}
function SubType(name, age) {
//继承属性
SuperType.call(this,name);
this.age = age;
}
//继承方法
inheritPrototype(SubType,SuperType);
SubType.prototype.sayAge = function () {
console.log(this.age);
}
var instance = new SubType();
function myMammal = {
name : 'Herb the Mammal',
get_name : function () {
return this.name;
},
says : function () {
return this.saying || '';
}
}
var myCat = Object.create(myMammal);
myCat.name = 'Henrietta';
myCat.saying = 'meow';
myCat.purr = function (n) {
var i, s = '';
for (i = 0;i < n; i += 1) {
if(s) {
s += '-'
}
s += 'r';
}
return s;
}
myCat.get_name = function () {
return this.says + ' ' + this.name + this.says;
}
Function.prototype.method = function (name,func) {
this.prototype[name] = func;
return this;
}
// 工厂mammal函数
var mammal = function (spec) {
var that = {};
that.get_name = function () {
return spec.name;
}
that.says = function (spec) {
return spec.saying || '';
}
return that;
}
// 工厂cat函数(基于mammal的函数)
var cat = function (spec) {
spec.saying = spec.saying || 'meow';
var that = mammal(spec);
that.purr = function (n) {
var i, s = '';
for (i = 0; i < n; i += 1) {
if(s) {
s += '-';
}
s += 'r';
}
}
that.get_name = function () {
return that.says() + ' ' + spec.name + ' ' + that.says();
}
return that;
}
// 创建myCat对象
var myCat = cat({name: 'Henrietta'});
Object.method('superior',function (name) {
var that = this,
method = that[name];
return function () {
return method.apply(that, arguments)
}
})
// 工厂coolcat函数(基于cat函数)
var coolcat = function (spec) {
var that = cat(spec),
super_get_name = that.superior('get_name');
that.get_name = function (n) {
return 'like ' + super_get_name() + ' baby';
}
return that;
}
var myCoolCat = coolcat({name : 'Bix'});
var name = myCoolCat.get_name();
var singleton = function () {
// 私有变量和函数
var privateVariable = 10;
function privateFunction () {
return false;
}
//特权/公有方法和属性
return {
publicProvperty: true;
publicMethod: function () {
privateVariable++;
return privateFunction();
}
}
}
var singleton = function () {
// 私有变量和函数
var privateVariable = 10;
function privateFunction () {
return false
}
// 创建对象
var object = new CustomType();
// 添加特权/公有属性和方法
object.publicProperty = true;
object.publicMethod = function () {
privateVariable++;
return privateFunction();
}
return object;
}()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有