PS:因为所有的设计模式都是基于面向对象来完成的,所以在讲解设计模式之前先来过一下面向对象都有哪些知识点

搭建开发环境

初始化npm环境

下载安装nodejs安装即可,nodejs自带npm管理包,然后使用下面命令检查node和npm的版本

创建项目的文件夹,然后再项目目录下使用npm init --yes生成package.json文件

安装webpack和配置webpack

在项目目录下,使用如下命令进行安装

  1. npm install --save-dev webpack
  1. npm install --save-dev webpack-cli

安装完成后项目中的package.json文件就有相关的配置

在项目根目录创建webpack.dev.config.js文件

  1. module.exports = {
  2. entry: './src/index.js', //项目入口文件
  3. output: {
  4. path: __dirname, // 当前目录
  5. filename: "./release/bundle.js" // 运行的时候自己会创建出来
  6. }
  7. }

配置package.json的scripts对象,新增一个dev

  1. "scripts": {
  2. "test": "echo \"Error: no test specified\" && exit 1",
  3. "dev": "webpack --config ./webpack.dev.config.js --mode development"
  4. },

mode development表示设置成开发者模式

然后执行那npm run dev

这样我们webpack就配置好了

安装webpack-dev-server和html-webpack-plugin

  1. npm install webpack-dev-server html-webpack-plugin --save-dev

在根目录创建一个index.html

然后在webpack.dev.config.js文件配置html模板和启动服务的配置

  1. const path = require('path')
  2. const HtmlWebpackPlugin = require('html-webpack-plugin')
  3.  
  4. module.exports = {
  5. entry: './src/index.js', //项目入口文件
  6. output: {
  7. path: __dirname, // 当前目录
  8. filename: "./release/bundle.js" // 运行的时候自己会创建出来
  9. },
  10. plugins: [
  11. new HtmlWebpackPlugin({
  12. template: "./index.html"
  13. })
  14. ],
  15. devServer: {
  16. contentBase: path.join(__dirname, './release'), // 配置本地执行环境的根目录
  17. open: true, //执行命令浏览器自动打开
  18. port: 9000
  19. }
  20. }

修改package.json的script下的dev

  1. "scripts": {
  2. "test": "echo \"Error: no test specified\" && exit 1",
  3. "dev": "webpack-dev-server --config ./webpack.dev.config.js --mode development"
  4. },

然后再执行npm run dev

安装babel,解析ES6

  1. npm install babel-core babel-loader babel-polyfill babel-preset-es2015 babel-preset-latest --save-dev

在项目根目录创建.babelrc文件

  1. {
  2. "presets": ["es2015", "latest"],
  3. "plugins": []
  4. }

在dev.webpack.config.js文件中配置module

  1. module: {
  2. rules: [{
  3. test: /\.js?$/,
  4. exclude: /(node_modules)/, // 略过
  5. loader: 'babel-loader'
  6. }]
  7. },

然后我们使用ES6语法测试babel是否配置成功

  1. class Person {
  2. constructor (name) {
  3. this.name = name
  4. }
  5. getName () {
  6. return this.name
  7. }
  8. }
  9.  
  10. let p = new Person('jack')
  11. alert(p.getName())

运行显示,已经正确执行index.js中的代码了

什么是面向对象

面向对象类的概念

  1. class Person {
  2. constructor (name, age) {
  3. this.name = name
  4. this.age = age
  5. }
  6. getName () {
  7. return this.name
  8. }
  9. speak () {
  10. return `My name is ${this.name}, age${this.age}`
  11. }
  12. }

面向对象实例化对象

  1. let p1 = new Person('jack', 23)
  2. let p2 = new Person('jack',32)
  3. alert(p1.getName())
  4. alert(p2.speak())

面向对象之继承

定义一个人的父类Person

  1. // 父类Person
  2. class Person {
  3. constructor (name, age) {
  4. this.name = name
  5. this.age = age
  6. }
  7. getName () {
  8. return this.name
  9. }
  10. speak () {
  11. return `My name is ${this.name}, age${this.age}`
  12. }
  13. }

定义一个Student子类继承父类Person

  1. // 子类,学生
  2. class Student extends Person{
  3. constructor (name, age, number) {
  4. super(name, age)
  5. this.number = number
  6. }
  7. // 子类特有的方法
  8. study () {
  9. return '我正在学习'
  10. }
  11. // 覆盖父类的方法
  12. speak() {
  13. return `My name is ${this.name}, age${this.age}, number${this.number}`
  14. }
  15. }

实例化子类Student

  1. let s1 = new Student('小明', 11, '1234567890')
  2. let s2 = new Student('小光', 12, '0987654321')
  3. alert(s1.study())
  4. alert(s2.speak())

Person类是公共的不仅仅服务于Student,继承可以将公共方法和属性抽离出来,提高复用性,减少冗余

面向对象之封装

面向对象的封装特性中有三个关键字需要理解的:public 完全开发,protected 对子类开发,private只对自己开发(可惜ES6中还不支持,可以使用typescript来演示)

ES6还不支持,一般认为_开头的属性是私有属性(private)

  1. // 父类Person
  2. class Person {
  3. name
  4. age
  5. protected weight // 定一个体重的属性,子类可以访问
  6. constructor (name, age, weight) {
  7. this.name = name
  8. this.age = age
  9. this.weight = weight
  10. }
  11. getName () {
  12. return this.name
  13. }
  14. speak () {
  15. return `My name is ${this.name}, age${this.age}, weight${this.weight}`
  16. }
  17. }
  18.  
  19. // 子类,学生
  20. class Student extends Person{
  21. number
  22. private girlfriend // 私有属性,只有Students可以访问
  23. constructor (name, age, number, weight, girlfriend) {
  24. super(name, age, weight)
  25. this.number = number
  26. this.girlfriend = girlfriend
  27. }
  28. study () {
  29. return '我正在学习'
  30. }
  31. getWeight () {
  32. return `My weight is ${this.weight}`
  33. }
  34. getGirlfriend () {
  35. return `My girlfriend is ${this.girlfriend}` // girlfriend属性是私有的,外面不能访问,只能提供一个方法
  36. }
  37. speak() {
  38. return `My name is ${this.name}, age${this.age}, number${this.number}`
  39. }
  40. }
  41.  
  42. let s1 = new Student('小明', 11, '1234567890', 123, 'xialli')
  43. let s2 = new Student('小光', 12, '0987654321',123, 'xiaohong')
  44. alert(s1.getWeight())
  45. alert(s2.getGirlfriend())

数据的权限和保密,不让外部程序看见,只暴露一些方法供外部程序调用

面向对象之多态

同一接口不同实现

  1. // 父类Person
  2. class Person {
  3. constructor (name, age) {
  4. this.name = name
  5. this.age = age
  6. }
  7. speak () { // 父类中的speak方法不写具体的实现,由子类来覆盖实现
  8.  
  9. }
  10. }
  11.  
  12. // 子类,学生
  13. class Student extends Person{
  14. constructor (name, age, number) {
  15. super(name, age)
  16. this.number = number
  17. }
  18. // 覆盖父类的方法
  19. speak() {
  20. return `My name is ${this.name}, age${this.age}, number${this.number}`
  21. }
  22. }
  23.  
  24. // 子类,学生
  25. class Teacher extends Person{
  26. constructor (name, age) {
  27. super(name, age)
  28. }
  29. // 覆盖父类的方法
  30. speak() {
  31. return `My name is ${this.name}, age${this.age}, I m Teacher`
  32. }
  33. }
  34.  
  35. let s1 = new Student('小明', 11, '1234567890')
  36. alert(s1.speak())
  37. let t1 = new Teacher('大明', 30)
  38. alert(t1.speak())

面向对象的应用案例

模拟Jquery的写法,理解Jquery是如何用面向对象实现的

  1. class jQuery {
  2. constructor(seletor) {
  3. let slice = Array.prototype.slice
  4. let dom = slice.call(document.querySelectorAll(seletor))
  5. let len = dom ? dom.length : 0
  6. for(let i = 0; i < len; i++){
  7. this[i] = dom[i]
  8. }
  9. this.length = len
  10. this.seletor = seletor || ''
  11. }
  12. append(node){
  13.  
  14. }
  15. addClass(name){
  16.  
  17. }
  18. html(data){
  19.  
  20. }
  21. }
  22. window.$ = function (selector) {
  23. // 工厂模式
  24. return new jQuery(selector)
  25. }
  26.  
  27. // 测试代码
  28. var $p = $('p')
  29. console.log($p)
  30. console.log($p.addClass)

在index.html中添加几个p标签测试

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>JavaScript设计模式</title>
  6. </head>
  7. <body>
  8. <p>JavaScript设计模式1</p>
  9. <p>JavaScript设计模式2</p>
  10. <p>JavaScript设计模式3</p>
  11. </body>
  12. </html>

面向对象的总结

基本上我们程序员写的业务代码都是这样的一个情况:程序执行—顺序,判断,循环——结构化

为何使用面向对象,其实面向对象就是数据结构化,将世界万物集合到对象中利于管理和封装,复用

UML类图

什么是UML类图:Unified Modeling Language 统一建模语言,UML可以包含很多种图,本系列种使用的是类图

画图工具:MS Office visio(比较复杂的类图) 和processon(本系列种使用这种)

创建类图的格式如下:

接下来我们使用processon来画一个People的类图

根据这个UML类图编写代码

  1. class People {
  2. constructor (name ,age) {
  3. this.name = name
  4. this.age = age
  5. }
  6. eat () {
  7. alert(`${this.name} eat something`)
  8. }
  9. speak () {
  10. alert(`My name is ${this.name}, age:${this.age}`)
  11. }
  12. }

接下来来设计一个比较复杂一点的类,类之间有各种关系的

泛化:表示继承,关联:表示引用

  1. class People {
  2. constructor (name ,house) {
  3. this.name = name
  4. this.age = house
  5. }
  6. saySomething () {}
  7. }
  8.  
  9. class A extends People {
  10. constructor(name, house){
  11. super(name, house)
  12. }
  13. saySomething () {
  14. alert('I am A')
  15. }
  16. }
  17.  
  18. class B extends People{
  19. constructor(name, house){
  20. super(name, house)
  21. }
  22. saySomething () {
  23. alert('I am B')
  24. }
  25. }
  26.  
  27. class House {
  28. constructor(city) {
  29. this.city = city
  30. }
  31. showCity () {
  32. alert(`house in ${this.city}`)
  33. }
  34. }

  35. // 测试代码
  36. let aHouse = new House('深圳')
  37. let a = new A('a', aHouse)
  38. console.log(a) // a有房子
  39.  
  40. let b = new B('b')
  41. console.log(b) // b没有房子

Bootstrap_CSS全局样式的更多相关文章

  1. BootStrap入门教程 (一) :手脚架Scaffolding(全局样式(Global Style),格网系统(Grid System),流式格网(Fluid grid System),自定义(Customing),布局(Layouts))

    2011年,twitter的“一小撮”工程师为了提高他们内部的分析和管理能力,用业余时间为他们的产品构建了一套易用.优雅.灵活.可扩展的前端工具集--BootStrap.Bootstrap由MARK ...

  2. bootstrap复习:全局样式

    一.概览 1.Bootstrap 是移动设备优先的. 2.在移动设备浏览器上,通过为视口(viewport)设置 meta 属性为 user-scalable=no 可以禁用其缩放(zooming)功 ...

  3. styled-components解决全局样式'injectGlobal' 废除的问题

    最新版的 styled-components v4 已经将原有的 injectGlobal() 方法替换成了 createGlobalStyle() ,而且用法也和之前的 injectGlobal 方 ...

  4. bootstrap 全局样式设置

    HTML <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" co ...

  5. 全局css , 样式设置, css 初始化. css ,style ,全局样式, 初始化样式

    全局CSS设置总结 1.清除所有标记的内外边距 html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4, h5, h6, form, fieldse ...

  6. taro 组件的外部样式和全局样式

    自定义组件对应的样式文件,只对该组件内的节点生效. 编写组件样式时,需要注意以下几点: 组件和引用组件的页面不能使用 id 选择器(#a).属性选择器([a])和标签名选择器,请改用 class 选择 ...

  7. 关于微信小程序中组件和页面对全局样式的继承性

    1.组件只能继承全局样式中的font和color(backgroundcolor不继承) 2.页面可以继承全局样式中所有样式

  8. bootstrap基础学习小记(一)简介模板、全局样式

    2011年,twitter的“一小撮”工程师为了提高他们内部的分析和管理能力,用业余时间为他们的产品构建了一套易用.优雅.灵活.可扩展的前端工具集--BootStrap.Bootstrap由MARK ...

  9. 02 uni-app框架学习:设置全局样式统一每个页面的背景颜色

    1.设置全局样式可以在App.vue里面 2.在每个页面的根view 里添加一个class名叫page

随机推荐

  1. ACM入门记

    [2015.12]零基础进队 [2016.4.10]浙大第十六届程序设计大赛 [2016.6.4]团体程序设计天梯赛初赛 [2016.7.16]团体程序设计天梯赛决赛 赛后总结:比赛的时候好慌,一道题 ...

  2. Spring.net Could not load type from string value

    最近有点懒了啊,都没有按时上来博客园更新下,个人觉得遇到难题的时候在这里留下脚印也亦造福他人,进来 晓镜水月 被项目围的团团转,asp.net MVC项目来的,但是我还是不务正业啊,在弄网络爬虫,这个 ...

  3. Create Script Template In Edit Mode

    很多时候 许多类 的 格式 都是重复的,比如 从配置文件中映射出来的类. 这个时候写一个 类模板 就很节省时间了. Code public static string TestPath = " ...

  4. Css 单图片按钮实例(css 图片变换)

    1.场景描述,根据鼠标的移动,动态的切换按钮图片. 2.方法1,准备两张120*41的图片,一张正常状态图片,一张按下效果图片.在鼠标放在的按钮上设置按下图片,移开又恢复到正常状态图片.缺点:在网页上 ...

  5. 九个衡量 Rails 应用性能的小方法

    你有个绝佳的商业创意,日复一日地将它完善丰满起来.后来,你雇了一群天赋异禀的开发者.Web 设计师和用户体验专家,他们用一种非常棒的框架--Ruby on Rails 帮你实现长久以来的梦想. 你的网 ...

  6. 转 wince程序 中使用Listview显示图标问题 (C#) .

    思路: 1.窗体控件:lstaqgl [Listview控件名称]  imageList1[ImageList控件] 2.  图片路径添加到—imageList1——Listview显示图片从 ima ...

  7. IIS 发布程序的一些心得

    1.应用程序池一般自己建立对应Framework版本的程序池,并托管管道模式为经典 2.在IIS根目录双击,右侧的“ISAPI和CGI限制” 双击打开,将自己所需要的Framework版本的限制设置为 ...

  8. BZOJ 1877: [SDOI2009]晨跑 费用流

    1877: [SDOI2009]晨跑 Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一 ...

  9. pku 1182(种类并查集)

    题目链接:http://poj.org/problem?id=1182 解题思路来自discuss:http://poj.org/showmessage?message_id=152847 #incl ...

  10. 8天学通MongoDB——第一天 基础入门

    原文地址:http://www.cnblogs.com/huangxincheng/archive/2012/02/18/2356595.html 关于mongodb的好处,优点之类的这里就不说了,唯 ...