分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

js封装(常见)

发布时间:2023-09-06 01:10责任编辑:沈小雨关键词:jsjs封装
var Person ={ ???name :‘alan‘, ???sayHello:function(){ ???????alert(‘hello ‘+ this.name); ???}};var p = Object.create(Person);console.log(p);p.sayHello();

js封装(常见)

function Person (info){ ???this._init_(info);}Person.prototype ={ ???constructor:Person, ???_init_:function(info){ ???????this.names = info.name; ???????this.age = info.age; ???????this.gender = info.gender; ???}, ???sayHello:function(){ ???????// console.log(‘hello‘); ???????alert(‘hello‘); ???} ???// demo:function(){},};var clzhang = new Person({ ???name:‘clzhang‘, ???age:‘1111‘, ???gender:‘‘});clzhang.sayHello()//hello

巧妙

// 类jQuery 封装var Person = function (info){ ???return new Person.prototype.init(info);};Person.prototype ={ ???constructor:Person, ???init:function(info){ ???????this.names = info.name; ???????this.age = info.age; ???????this.gender = info.gender; ???}, ???sayHello:function(){ ???????// console.log(‘hello‘); ???????alert(‘hello2‘); ???} ???// demo:function(){},};Person.prototype.init.prototype = Person.prototype;var clzhang = new Person({ ???name:‘clzhang‘, ???age:‘1111‘, ???gender:‘‘});clzhang.sayHello()//hello2

闭包

var Person = (function(window){ ???var Person = function (info){ ???return new Person.prototype.init(info);};Person.prototype ={ ???constructor:Person, ???init:function(info){ ???????this.names = info.name; ???????this.age = info.age; ???????this.gender = info.gender; ???}, ???sayHello:function(){ ???????// console.log(‘hello‘); ???????alert(‘hello3‘); ???} ???// demo:function(){},};Person.prototype.init.prototype = Person.prototype;return Person;})();var clzhang = ?Person({ ???name:‘clzhang‘, ???age:‘1111‘, ???gender:‘‘});clzhang.sayHello()//hello3

js封装(常见)

原文地址:http://www.cnblogs.com/alan-alan/p/7501626.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved