随笔分类 -  Javascript/JQuery Deep Inside

Talk about advanced features of Javascript and JQuery
摘要:在一个函数体内,标识符arguments具有特殊含义.Arguments对象是一个类似数组的对象eg:验证函数参数的正确数目function f(x, y, z) { if (arguments.length != 3) { throw new Error("function with " + arguments.length + "arguments, but it expects 3 arguments.") // now do the actual function } }eg:简单的max函数能接受任意数目的实际参数function ... 阅读全文
posted @ 2013-10-03 10:35 Master HaKu 阅读(4885) 评论(0) 推荐(0) 编辑
摘要:1. Basic Selectors$('p')—Accesses all the paragraph elements in the HTML file $('div')—Accesses all the div elements in the HTML file $('#A')—Accesses all the HTML elements with id=A $('.b')—Accesses all the HTML elements with class=b2.Applying CSS to Elements$('d 阅读全文
posted @ 2013-10-02 17:32 Master HaKu 阅读(237) 评论(0) 推荐(0) 编辑
摘要:1. SerializeJavaScript object to JSONvar messageObject = { title: 'Hello World!', body: 'It\'s great!' };var serializedJSON = JSON.stringify( messageObject ); });2. Parse JSON to Javascript Objectvar serializedJSON = '{"title":"Hello World!","body&quo 阅读全文
posted @ 2013-10-02 17:07 Master HaKu 阅读(404) 评论(0) 推荐(0) 编辑
摘要:1.什么是 prototypeprototype 对于 JavaScript 的 意义重大,prototype 不仅仅是一种管理对象继承的机制,更是一种出色的设计思想在现实生活中,我们常常说,某个东西是以另一个东西为原型创作的。这两个东西可以是同一个类型, 也可以是不同类型。习语“照猫画虎”,这里的猫就是原型,而虎就是类型,用 JavaScript 的 prototype 来 表示就是“虎.prototype =某只猫”或者“虎.prototype= new 猫()”eg:function ClassA(){...... }ClassA.prototype = new Object(); fu 阅读全文
posted @ 2013-10-02 11:11 Master HaKu 阅读(241) 评论(0) 推荐(0) 编辑
摘要:bad code// BEFORE: 5 globals// Warning: antipattern// constructorsfunction Parent() {}function Child() {}// a variablevar some_var = 1;// some objectsvar module1 = {};module1.data = {a: 1, b: 2};var module2 = {};good code// AFTER: 1 global// global objectvar MYAPP = {};// constructorsMYAPP.Parent = 阅读全文
posted @ 2013-09-22 15:07 Master HaKu 阅读(510) 评论(0) 推荐(1) 编辑
摘要:This pattern is useful when your function has some initial preparatory work to do andit needs to do it only once.In such cases, the selfdefining function can update its own implementation.eg:var selfFunc = function () { console.log("First Initialization!"); selfFunc = function () { ... 阅读全文
posted @ 2013-09-22 13:10 Master HaKu 阅读(480) 评论(0) 推荐(0) 编辑
摘要:overriding the default options with user-supplied options and the jQuery extend() methodeg:$.fn.pulse = function (options) { // Merge passed options with defaults var opts = $.extend({}, $.fn.pulse.defaults, options); return this.each(function () { // Pulse for (var i = 0; i <... 阅读全文
posted @ 2013-09-21 15:19 Master HaKu 阅读(294) 评论(0) 推荐(0) 编辑
摘要:1. How do you write a plugin in jQuery?You can extend the existing jQuery object by writing either methods or functions.1) Writing a custom jQuery methodjQuery methods are defined by extending the jQuery.fn object with your method name.$.fn.extend({ //Only Test MasterTest: { alertTest: ... 阅读全文
posted @ 2013-09-21 15:01 Master HaKu 阅读(272) 评论(0) 推荐(0) 编辑
摘要:1. 动态添加对象的属性和方法// start with an empty objectvar dog = {};// add one property dog.name = "Benji";// now add a method dog.getName = function () { return dog.name; };2. 一次性创建对象var dog = { name: "Benji", getName: function () { return this.name; }};3. 使用内建的构造函数// one way -- using a li 阅读全文
posted @ 2013-09-19 16:48 Master HaKu 阅读(238) 评论(0) 推荐(0) 编辑
摘要:JavaScript is a class-less language, however classes can be simulated using functions.eg:// A car 'class'function Car(model) { this.model = model; this.color = 'silver'; this.year = '2012'; this.getInfo = function () { return this.model + ' ' + this.year; }}We can the 阅读全文
posted @ 2013-09-01 16:34 Master HaKu 阅读(368) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示