-
Notifications
You must be signed in to change notification settings - Fork 0
zhoumingjun edited this page Jun 3, 2019
·
1 revision
- Object prototype
spacify('hello world') // => 'h e l l o w o r l d'
function spacify(str) {
return str.split('').join(' ');
}
String.prototype.spacify = function(){
return this.split('').join(' ');
};
- Arguments log('hello world');
function log(msg){
console.log(msg);
}
function log(){
console.log.apply(console, arguments);
};
function log(){
var args = Array.prototype.slice.call(arguments);
args.unshift('(app)');
console.log.apply(console, args); };
- Context When a function is called as a method of an object, this is set to the object the method is called on: when invoking a function with the new operator to create an instance of an object. When invoked in this manner, the value of this within the scope of the function will be set to the newly created instance: When called as an unbound function, this will default to the global context or window object in the browser. However, if the function is executed in strict mode, the context will default to undefined.
var User = {
count: 1,
getCount: function() { return this.count; } };
console.log(User.getCount());
var func = User.getCount;
console.log(func());
Function.prototype.bind = Function.prototype.bind || function(context) {
var self = this;
return function(){
return self.apply(context, arguments);
}; }
function foo() { alert(this); }
<<AllPages()>>