面向对象编程
创建对象
JavaScript对每个会设置一个原型,只想他的原型对象,当我们用obj.xxx访问一个对象1的属性时,JavaScript引擎现在当前对象查找该属性,如果没有找到,就到其原型对象上找,如果还没有找到,就一直上溯到object.prototype对象,最后,如果还没有找,就只能返回undefined
例如:创建一个Array对象:
var arr=[1,2,3];
其原型链是: arr–>Array.prototype–>Object.prototype–>null
Array.prototype定义了indexOf(),shift()等方法,因此你可以在所有的Array对象上直接调用这些方法
//当我们创建一个函数时:
function foo(){
return 0;
}
//函数也是一个对象,它的原型链是:
// foo—>Function.prototype—>Object.prototype---->null
/* 由于Function.prototype定义了apply()等方法,因此,所有函数都可以调用apply()方法
很容易想到,如果原型链很长,那么访问一个对象的属性就会因为花更多的时间查找而变得更慢,因此要注意不要把原型链搞得太长 */
//构造函数
//除了直接用{…}创建一个对象外,Javascript还可以用一种构造函数的方法来创建对象,他的用法是,先定一个构造函数
function Student(name){
this.name=name;
this.hello=function(){
console.log(‘Hello:’+this.name+’!’);
}
}
var xiaoming=new Student(‘小明’)
console.log(xiaoming.name)
//用 new Student()创建的对象还从原型上获得一个constructor属性,它指向函数Student本身
console.log(xiaoming.constrcuctorStudent.prototype.constrcuctor)
console.log(Student.prototype.constrcuctorStudent)
console.log(Object.getPrototypeOf(xiaoming)===Student.prototype)
console.log(xiaoming instanceof Student)
//我们必须借助一个中间对象来实现正确的原型链,这个中间对象的原型要指向Student.prototype.为了实现这一点,可以借助一个空函数F来实现
//promaryStudent构造函数
function PrimaryStudent(props){
Student.call(this,props);
this.grade=props.grade||1;
}
//空函数F
function F(){
}
//把F的原型指向Student.prototype
F.prototype=Student.prototype
//把PrimaryStudent的原型指向一个新的F对象,F对象的原型正好指向Student.prototype;
PrimaryStudent.prototype=new F()
//把PrimaryStudent原型的构造函数修复为PrimaryStudent
PrimaryStudent.prototype.constructor=PrimaryStudent;
//继续在PrimaryStdudent原型(就是new F()对象)上定义方法
PrimaryStudent.prototype.getGrade=function(){
return this.grade;
}
//创建xiaoming
var xiaoming=new PrimaryStudent({
name:'小明',
grade:2
});
console.log(xiaoming.name)
console.log(xiaoming.grade)
//验证原型
console.log(xiaoming.__proto__==PrimaryStudent.prototype)
console.log(xiaoming.__proto__.__proto__===Student.prototype)
//验证继承关系
console.log(xiaoming instanceof PrimaryStudent)
console.log(xiaoming instanceof Student)
//注意函数 F 仅用于桥接,我们仅创建一个 new F()实例,而且,没有改变原有的Student定义的原型链
// 如果把继承这个动作用一个 inherits() 函数封装起来,还可以隐藏F的定义,并简化代码
function inherits(Child,Parent){
var F=function(){}
F.prototype=Parent.prototype;
Child.prototype=new F();
Child.prototype.constrcuctor=Child;
}
//这个inherits()函数可复用
function Student(props){
this.name=props.name||'Unnamed';
}
Student.prototype.hello=function(){
alert('Hello,'+this.name+'!')
}
function promaryStudent(props){
Student.call(this.props);
this.grade=props.grade||1;
}
//实现原型继承链
inherits(promaryStudent,Student);
//绑定其他方法到PrimaryStudent原型
promaryStudent.prototype.getGrade=function(){
return this.grade;
}
JavaScript 的原型继承实现方式是:
1.定义新的构造函数,并在内部用call()调用希望“继承”的构造函数,并绑定this;
2.借助中间函数F实现原型链继承,最好通过封装的inherits函数完成
3.继承在新的构造函数的原型上定义新方法
*/
</script>
<script>
function A(a){
this.a=a;
}
console.log(A.prototype)
console.log(A.prototype.__proto__)
console.log(Object.prototype)
带尺寸的图片:
居中的图片:
居中并且带尺寸的图片:
当然,我们为了让用户更加便捷,我们增加了图片拖拽功能。
如何插入一段漂亮的代码片
去博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片
.
// An highlighted block
var foo = 'bar';
生成一个适合你的列表
- 项目
- 项目
- 项目
- 项目
- 项目1
- 项目2
- 项目3
- 计划任务
- 完成任务
创建一个表格
一个简单的表格是这么创建的:
项目 | Value |
---|---|
电脑 | $1600 |
手机 | $12 |
导管 | $1 |
设定内容居中、居左、居右
使用:---------:
居中
使用:----------
居左
使用----------:
居右
第一列 | 第二列 | 第三列 |
---|---|---|
第一列文本居中 | 第二列文本居右 | 第三列文本居左 |
SmartyPants
SmartyPants将ASCII标点字符转换为“智能”印刷标点HTML实体。例如:
TYPE | ASCII | HTML |
---|---|---|
Single backticks | 'Isn't this fun?' | ‘Isn’t this fun?’ |
Quotes | "Isn't this fun?" | “Isn’t this fun?” |
Dashes | -- is en-dash, --- is em-dash | – is en-dash, — is em-dash |
创建一个自定义列表
-
Markdown
- Text-to- HTML conversion tool Authors
- John
- Luke
如何创建一个注脚
一个具有注脚的文本。1
注释也是必不可少的
Markdown将文本转换为 HTML。
KaTeX数学公式
您可以使用渲染LaTeX数学表达式 KaTeX:
Gamma公式展示 Γ ( n ) = ( n − 1 ) ! ∀ n ∈ N \Gamma(n) = (n-1)!\quad\forall n\in\mathbb N Γ(n)=(n−1)!∀n∈N 是通过欧拉积分
Γ ( z ) = ∫ 0 ∞ t z − 1 e − t d t   . \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,. Γ(z)=∫0∞tz−1e−tdt.
你可以找到更多关于的信息 LaTeX 数学表达式here.
新的甘特图功能,丰富你的文章
- 关于 甘特图 语法,参考 这儿,
UML 图表
可以使用UML图表进行渲染。 Mermaid. 例如下面产生的一个序列图::
这将产生一个流程图。:
- 关于 Mermaid 语法,参考 这儿,
FLowchart流程图
我们依旧会支持flowchart的流程图:
- 关于 Flowchart流程图 语法,参考 这儿.
导出与导入
导出
如果你想尝试使用此编辑器, 你可以在此篇文章任意编辑。当你完成了一篇文章的写作, 在上方工具栏找到 文章导出 ,生成一个.md文件或者.html文件进行本地保存。
导入
如果你想加载一篇你写过的.md文件,在上方工具栏可以选择导入功能进行对应扩展名的文件导入,
继续你的创作。
注脚的解释 ↩︎