这篇文章主要为大家详细介绍了js面向对象菜鸟教程,具有一定的参考价值,可以用来参考一下。
本指南可以很快让你学会写优美的面向对象JavaScript代码,我保证!学会写简洁的JavaScript代码对一个开发者的发展很重要,随着像Node.js这类技术的出现,你现在可以在服务器端写JavaScript代码了,你甚至可以用JavaScript来查询像MongoDB这样的持久性数据存储。
// @arrange (512.笔记) www.q1010.com
var bill = {};
// @arrange (512.笔记) www.q1010.com
bill.name = "Bill E Goat";
bill.sound = function() {
console.log( 'bahhh!' );
};
// @arrange (512.笔记) www.q1010.com
var bill = {
name: "Bill E Goat", sound: function() { console.log( 'bahhh!' ); }
};
// @arrange (512.笔记) www.q1010.com
bill.name; // "Bill E Goat"
bill.sound(); // "bahhh"
// @arrange (512.笔记) www.q1010.com
bill['name']; // "Bill E Goat"
请注意当调用一个方法时我们要在方法名后面添加一对括号去调用它。让我们重写当前的sound方法并传给它一个参数来调用它:
// @arrange (512.笔记) www.q1010.com
bill.sound = function(noise) {
console.log( noise);
};
bill.sound("brrr!"); // "brrr!" He's cold :)
// @arrange (512.笔记) www.q1010.com
bill.sayName = function() {
console.log( "Hello " + this.name );
};
bill.sayName(); // "Hello Bill E Goat"
// @arrange (512.笔记) www.q1010.com
bill.sayName; // function
// @arrange (512.笔记) www.q1010.com
var sound = bill.sound;
sound('moo!'); // "moo!"
// @arrange (512.笔记) www.q1010.com
var sally = bill;
sally.name; // "Bill E Goat", But her name is Sally silly
sally.name = "Sally";
sally.name; // "Sally", Better
bill.name; // "Sally", Oh no what happened to Bill
// @arrange (512.笔记) www.q1010.com
bill.name = "Bill E Goat";
bill.sayName(); // "Hello Bill E Goat";
var sayName = bill.sayName;
sayName; // function, OK so far so good
sayName(); // "Hello ", huh why didn't it print out Bills name?
// @arrange (512.笔记) www.q1010.com
var name = "Bearded Octo";
sayName(); // "Hello Bearded Octo"
// @arrange (512.笔记) www.q1010.com
function Game() {};
// @arrange (512.笔记) www.q1010.com
var zelda = new Game();
var smb = new Game();
zelda.title = "Legend of Zelda";
smb.title = "Super Mario Brothers";
zelda.title; // "Legend of Zelda"
smb.title; // "Super Mario Brothers"
// @arrange (512.笔记) www.q1010.com
function Game(title) {
this.title = typeof title !== 'undefined' ? title : "";
};
var zelda = new Game("Legend of Zelda");
zelda.title; // "Legend of Zelda"
zelda.title = "Ocarina of Time";
zelda.title; // "Ocarina of Time"
var blank = new Game();
blank.title; // ""
// @arrange (512.笔记) www.q1010.com
if (typeof title !== 'undefined') {
this.title = title;
} else {
this.title = "";
}
// Is the same as
this.title = typeof title !== 'undefined' ? title : "";
// @arrange (512.笔记) www.q1010.com
zelda.loveTitle = function() {
console.log( "I love " + this.title );
};
zelda.loveTitle(); // "I love Ocarina of Time"
// @arrange (512.笔记) www.q1010.com
Game.prototype.heartIt = function() {
console.log( "I heart " + this.title );
};
zelda.heartIt(); // "I heart Ocarina of Time"
smb.heartIt(); // "I heart Super Mario Brothers"
结束,顺便说一句:这种短小精悍的国外优秀教材最喜欢了~
本文来自:http://www.q1010.com/174/2075-0.html
注:关于js面向对象菜鸟教程的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:面向对象
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。