美文网首页语言基础
原型操作v1.0.0

原型操作v1.0.0

作者: 一点金光 | 来源:发表于2019-07-28 00:12 被阅读0次
---
title: 原型操作
date: 2018-06-09 16:29:00
updated: 2018-06-10 12:00:00
categories:
- 网页开发
- 开发应用
- 对象编程
- 函数编程
tags:
- nodejs
---

针对浏览器端的Object的原型进行操作
现在已经不提倡做直接操作dom

//显示/隐藏
//hide()
Object.prototype.hide = function() {
    this.style.display = "none";
    return this;
}
//show() 
Object.prototype.show = function() {
    this.style.display = "block";
    return this;
}

//滑动
//slideDown() 
Object.prototype.slideDown = function() {
    this.style.display = 'block';
    if (this.clientHeight < this.scrollHeight) {
        this.style.height = 10 + this.clientHeight + "px";
        var _this = this;
        setTimeout(function() { _this.slideDown() }, 10)
    } else {
        this.style.height = this.scrollHeight + "px";
    }
}
//slideUp() 
Object.prototype.slideUp = function() {
    if (this.clientHeight > 0) {
        this.style.height = this.clientHeight - 10 + "px";
        var _this = this;
        setTimeout(function() { _this.slideUp() }, 10)
    } else {
        this.style.height = 0;
        this.style.display = 'none';
    }
}

//设置/获取属性
//attr() 
Object.prototype.attr = function() {
    if (arguments.length == 1) {
        return eval("this." + arguments[0]);
    } else if (arguments.length == 2) {
        eval("this." + arguments[0] + "=" + arguments[1]);
        return this;
    }
}
//val() 
Object.prototype.val = function() {
    if (arguments.length == 0) {
        return this.value;
    } else if (arguments.length == 1) {
        this.value = arguments[0];
        return this;
    }
}
//html() 
Object.prototype.html = function() {
    if (arguments.length == 0) {
        return this.innerHTML;
    } else if (arguments.length == 1) {
        this.innerHTML = arguments[0];
        return this;
    }
}

//css() 
Object.prototype.css = function() {
    if (arguments.length == 1) {
        return eval("this.style." + arguments[0]);
    } else if (arguments.length == 2) {
        eval("this.style." + arguments[0] + "='" + arguments[1] + "'");
        return this;
    }
}
// USING EVAL MEY BE NOT GOOD.!!!


//添加元素
//append() 
Object.prototype.append = function(newElem) {
    this.innerHTML += newElem;
    return this;
}
//prepend() 
Object.prototype.prepend = function(newElem) {
    this.innerHTML = arguments[0] + this.innerHTML;
    return this;
}
//after() 
Object.prototype.after = function(newElem) {
    this.outerHTML += arguments[0];
    return this;
}
//before() 
Object.prototype.before = function(newElem) {
    this.outerHTML = arguments[0] + this.outerHTML;
    return this;
}

//删除元素
//empty() 
Object.prototype.empty = function() {
    this.innerHTML = "";
    return this;
}
//替换元素
//replaceWith() 
Object.prototype.replaceWith = function(newElem) {
    this.outerHTML = arguments[0];
    return this;
}

//设置元素类名属性
//hasClass() 
Object.prototype.hasClass = function(cName) {
    return !!this.className.match(new RegExp("(\\s|^)" + cName + "(\\s|$)"));
}
//addClass() 
Object.prototype.addClass = function(cName) {
    if (!this.hasClass(cName)) {
        this.className += " " + cName;
    }
    return this;
}
//removeClass() 
Object.prototype.removeClass = function(cName) {
    if (this.hasClass(cName)) {
        this.className = this.className.replace(new RegExp("(\\s|^)" + cName + "(\\s|$)"), " ");
    }
    return this;
}
//I prefer delClass to removeClass
Object.prototype.delClass= function(cName) {
    if (this.hasClass(cName)) {
        this.className = this.className.replace(new RegExp("(\\s|^)" + cName + "(\\s|$)"), " ");
    }
    return this;
}

相关文章

  • 原型操作v1.0.0

    针对浏览器端的Object的原型进行操作现在已经不提倡做直接操作dom

  • 数组操作-v1.0.0

    目录 克隆扁平连接拼接截取排序添加删除遍历映射过滤某一符合所有符合包含生产转化复杂查找填充包含 正文 #克隆 #扁...

  • 函数操作v1.0.0

    目录 函柯里化函数节流函数防抖 正文 函柯里化 函数节流 函数防抖

  • 字符操作-v1.0.0

    目录 es5es6 正文 es5 es6

  • 对象操作-v1.0.0

    目录 有何特点如何创建如何继承何时多态 正文 有何特点抽象、封装、继承、多态 如何创建工厂、构造、原型、构造+原型...

  • 2018-11-26

    6.2.3 原型模式 1、理解原型对象 2、原型与in操作符 有两种方式使用in操作符:单独使用:in操作符会在通...

  • 浏端操作-v1.0.0

    title: 浏端操作date: 2018-06-09 16:29:00updated: 2018-06-10 1...

  • Git 删除github上某个tag/release

    github中删除release/tag只能在命令执行,不能界面点击操作 例子: 删除一个 v1.0.0 的rel...

  • 舟山医后付SDK集成文档v1.0.0

    舟山医后付SDK集成文档v1.0.0 名称: 舟山医后付SDK集成文档V1.0.0版本: V1.0.0作者: 宋珍...

  • JS基础回归01:new操作符,原型和原型链

    本篇介绍 new 操作符的背后原理以及 JS 如何依赖原型形成原型链,完成继承。 new 操作符的本质 new 操...

网友评论

    本文标题:原型操作v1.0.0

    本文链接:https://www.haomeiwen.com/subject/eunyrctx.html