JS:采摘自JS精粹
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- </head>
- <body>
- <script>
- //无论是变量名字还是,对象的属性都不能是 保留字;
- //继承
- Object.prototype.method = function(method,fn){
- return this[method] = fn;
- };
- //继承方法1 复制继承
- Function.prototype.method('new',function(){
- var that = Object.create(this.prototype);
- var other = this.apply(that, arguments);
- return (typeof other === 'object' && other) || that;
- });
- //继承方法2: 原型继承
- Object.prototype.method('inherits',function(parent){
- this.prototype = new parent();
- //return this;
- });
- function aa(){};
- aa.prototype.fn = function(){alert(1)};
- aa.prototype.fn1 = function(){alert(2)};
- //var bb = function(){}
- //bb.prototype = aa.new();
- //var bb = function(){};
- //bb.inherits( aa )
- //aa.fn1() //错误
- var bb = (new aa)
- bb.fn1() // alert(1);
- //函数的属性和prototype是不一样的,但是用new了函数以后,返回的obj拥有了protype(继承)的方法和属性
- //Model化开发
- var mammal = function(spec){
- var that = {};
- that.get_name = function(){
- return spec.name;
- };
- that.says = function(){
- return spec.saying || '';
- };
- return that;
- };
- var myMammal = mammal( {name : 'herb'} );
- //差异化,复用mammal
- var cat = function(spec){
- spec.saying = spec.saying || 'meow';
- var that = mammal(spec);
- that.purr = function(n){
- var i, s = '';
- for(var i=0; i<n; i++){
- if(s){
- s += '_';
- };
- s += 'r';
- };
- return s;
- };
- that.get_name = function(){
- return that.says() + ' ' +spec.name+ ' ' + that.says();
- };
- return that;
- };
- var myCat = cat({name : 'xx'});
- //部件__ ->_->话说自定义事件和常见啊 !>_<!
- var eventuality = function(that){
- var registry = {};
- that.fire = function(event){
- var array,
- func,
- handler,
- i,
- type = typeof event === 'string' ? event : event.type;
- if( registry.hasOwnProperty(type) ){
- array = registry[type];
- for(var i=0; i<array.length; i++){
- handler = array[i];
- func = handler.func;
- func();
- }
- };
- };
- that.on = function(type, method, parameters){
- //registry[type] || ( registry[type] = {} );
- var handler = {
- method : method,
- parameters : parameters
- };
- if( registry.hasOwnProperty(type) ){
- registry[type].push( handler );
- }else{
- registry[type] = [handler];
- };
- return this;
- };
- return that;
- }
- </script>
- </body>
- </html>
JS:采摘自JS精粹的更多相关文章
- JS模式:jq中简单的模式--》采摘自js设计(tomxu_version)
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- JS高级(摘自简书)
JS高级 1. 访问对象属性(方法也是属性)的通用方式:obj['属性名'] 1. 属性名包含特殊字符,如"-".空格,访问:obj['content-type'] 2. 属性名不 ...
- MVC、MVP、MVVM、Angular.js、Knockout.js、Backbone.js、React.js、Ember.js、Avalon.js、Vue.js 概念摘录
注:文章内容都是摘录性文字,自己阅读的一些笔记,方便日后查看. MVC MVC(Model-View-Controller),M 是指业务模型,V 是指用户界面,C 则是控制器,使用 MVC 的目的是 ...
- 再谈React.js实现原生js拖拽效果
前几天写的那个拖拽,自己留下的疑问...这次在热心博友的提示下又修正了一些小小的bug,也加了拖拽的边缘检测部分...就再聊聊拖拽吧 一.不要直接操作dom元素 react中使用了虚拟dom的概念,目 ...
- spring访问静态资源出错,No mapping found for HTTP request with URI xxx/resources/js/jquery.min.js...
问题:spring访问静态资源出错,No mapping found for HTTP request with URI xxx/resources/js/jquery.min.js... web.x ...
- prototype.js 和 jQuery.js中 ajax 的使用
这次还是prototype.js 和 jQuery.js冲突的问题,前面说到过解决办法http://www.cnblogs.com/Joanna-Yan/p/4836252.html,以及上网说的大部 ...
- LazyLoad.js及scrollLoading.js
http://blog.csdn.net/ning109314/article/details/7042829 目前图片延迟加载主要分两大块,一是触发加载(根据滚动条位置加载图片):二是自动预加载(加 ...
- JS引用另外JS文件的顺序问题。
1.在a.js中可以引用b.js文件,这样就可以在网页中只引用a.js文件,从而可以使用a.js和b.js文件中的所有方法. 引用格式如下:document.write('<script typ ...
- jquery的validate.js 和 form.js 的使用方法
在使用 Jquery 的方法的验证并且修改 原Form 表单的提交方式的时候,需要引用的文件有 <script type="text/javascript" src=&quo ...
随机推荐
- selenium如何解决IE自动填充表单问题
有时候用selenium会碰到自动填充表单的问题,如输入用户名后,密码自动填充,此时再填充密码会导致登录失败,解决办法:每个输入框都调用clear()方法
- 矩阵乘法快速幂 codevs 1732 Fibonacci数列 2
1732 Fibonacci数列 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 在“ ...
- NYOJ-301递推求值
递推求值 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给你一个递推公式: f(x)=a*f(x-2)+b*f(x-1)+c 并给你f(1),f(2)的值,请求出f ...
- java14-9 Doteformat的练习
需求: 键盘录入出生年月日,计算出距离现在已经生活了几天 分析: A:创建键盘录入固定模式的字符串 B:计算步骤: a:把输入进来的字符串格式化成日期 b:获取现在的日期,减去格式化后的日期 c:把得 ...
- Nginx反向代理+负载均衡简单实现(http方式)
1)nginx的反向代理:proxy_pass2)nginx的负载均衡:upstream 下面是nginx的反向代理和负载均衡的实例: 负载机:A机器:103.110.186.8/192.168.1. ...
- RDLC报表系列--------行分组报表
报表分组开发步骤: 先看总体效果:如图 下面就做个看看... 1.先将数据处理成如下结构 如图 2.创建数据集DataSet.xsd,创建表->右键选择添加数据表->添加行(ctrl+L ...
- 千份位Javascript Thousand Separator / string format
function Separator(str){ return str.split(/(\d+)(\d{3})(\d{3})(\d{3})(\d{3})/).join(',').replace(/^, ...
- C语言 函数理解(以数组做参数)
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> int run(int *p){ // ...
- U3D assetbundle打包
using UnityEngine; using System.Collections; using UnityEditor; //此脚本不一定要放于editor目录下,经测试,放于其它地方也可以 p ...
- wooyun本地数据抓取
---- #-*-coding:utf-8-*- import re import urllib import MySQLdb import time from urllib import unquo ...